« first day (2912 days earlier)      last day (36 days later) » 

11:15
@panadestein Interested in APL in addition to BQN?
 
4 hours later…
14:54
Hi everyone, I'm slowly making my way through Advent of Code, and I have a recurring problem that I was hoping you could help me overcome.
What follows is my solution for day 17 part 2. The "sim" function can be seen as a black box that outputs a number from 0 to 7, as the specifics are not relevant to the issue I'm encountering. I should also mention that index origin is set to 0

sim ← {B1 ← (3⍴2)⊤⍵ ⋄ B2 ← B1 ≠ 1 0 1 ⋄ C ← ¯3↑(¯1×(2⊥B2))↓⍺,B1 ⋄ B3 ← B2 ≠ 1 1 0 ⋄ 2⊥B3≠C}
iter ← {0=≢⍵: ⍺ ⋄ a ← ⍸(1↑⍵)=⍺∘sim¨⍳8 ⋄ a≡⍬: ⍬ ⋄ (⍺(,⍥⊂)⍵)∘{(⍺[0](,⍥∊)(3⍴2)⊤⍵)iter 1↓↑⍺[1]}¨a}
15:04
@Jack Hi Jack, and welcome to. First off, for next time, take note:
Dec 8, 2024 at 10:47, by Adám
@JohannesHoff (post APL code in separate messages and press Ctrl+k before posting; SE chat will visually merge your messages and format the code properly)
Also put backticks (`) around your inline code, cf. (↓((55÷⍨≢∊r) 55)⍴∊r) (with backticks) vs (↓((55÷⍨≢∊r) 55)⍴∊r) (without backticks).
15:22
@Jack One way to do it is to set up an accumulator:
sim ← {B1 ← (3⍴2)⊤⍵ ⋄ B2 ← B1 ≠ 1 0 1 ⋄ C ← ¯3↑(¯1×(2⊥B2))↓⍺,B1 ⋄ B3 ← B2 ≠ 1 1 0 ⋄ 2⊥B3≠C}
iter ← {0=≢⍵: r,←⊂⍺ ⋄ a ← ⍸(1↑⍵)=⍺∘sim¨⍳8 ⋄ a≡⍬: ⍬ ⋄ (⍺(,⍥⊂)⍵)∘{(⍺[0](,⍥∊)(3⍴2)⊤⍵)iter 1↓↑⍺[1]}¨a}
r ← ⍬ ⋄ (7⍴0) iter 0 3 5 5 3 4 3 0 6 1 5 7 5 1 4 2
After running this, r will have the vector-of-vectors result.
@Jack I can see two easy ways to avoid the explicit call to iter in an inner dfn. One is to make the inner function tacit:
iter ← {0=≢⍵: r,←⊂⍺ ⋄ a ← ⍸(1↑⍵)=⍺∘sim¨⍳8 ⋄ a≡⍬: ⍬ ⋄ (⍺(,⍥⊂)⍵)∘((⊃⍤⊣(,⍥∊)(3⍴2)⊤⊢)∇ 1↓1⊃⊣)¨a}
Btw, you can remove some redundant parens around (2⊥B2) and ,⍥⊂ and ,⍥∊ and (⍺,⍥⊂⍵) is the same as ⍺ ⍵
¯1× is just - and (1↑⍵) should be (⊃⍵)
Oh, and the other is to make the inner dfn into an operator to which you can then pass as operand. To make it easier to pass in ⍺ ⍵ for each of a, you might as well make it dyadic:
sim ← {B1 ← (3⍴2)⊤⍵ ⋄ B2 ← B1 ≠ 1 0 1 ⋄ C ← ¯3↑(-2⊥B2)↓⍺,B1 ⋄ B3 ← B2 ≠ 1 1 0 ⋄ 2⊥B3≠C}
iter ← {0=≢⍵: r,←⊂⍺ ⋄ a ← ⍸(⊃⍵)=⍺∘sim¨⍳8 ⋄ a≡⍬: ⍬ ⋄ ⍺ ⍵{(⍺⍺[0](,⍥∊)(3⍴2)⊤⍵)⍵⍵ 1↓↑⍺⍺[1]}∇¨a}
r ← ⍬ ⋄ (7⍴0) iter 0 3 5 5 3 4 3 0 6 1 5 7 5 1 4 2
@Jack For consistency, you could write ⍵≡⍬ instead of 0=≢⍵. Also, I'd use ,∘, instead of ,⍥∊ because the former does less, and is clearer about what is actually happening.
Now, I don't know what the various variables signify, but in sim, I'd reverse the order of the C← and B3← statements:
sim ← {B1 ← (3⍴2)⊤⍵ ⋄ B2 ← B1 ≠ 1 0 1 ⋄ B3 ← B2 ≠ 1 1 0 ⋄ C ← ¯3↑(-2⊥B2)↓⍺,B1 ⋄ 2⊥B3≠C}
I consider it good practice to have constants on the left of comparisons, so I'd write:
sim ← {B1 ← (3⍴2)⊤⍵ ⋄ B2 ← 1 0 1 ≠ B1 ⋄ B3 ← 1 1 0 ≠ B2 ⋄ C ← ¯3↑(-2⊥B2)↓⍺,B1 ⋄ 2⊥C≠B3}
iter ← {⍬≡⍵: r,←⊂⍺ ⋄ a ← ⍸(⊃⍵)=⍺∘sim¨⍳8 ⋄ ⍬≡a: ⍬ ⋄ ⍺ ⍵∘((⊃⍤⊣,∘,(3⍴2)⊤⊢)∇ 1↓1⊃⊣)¨a}
r ← ⍬ ⋄ (7⍴0) iter 0 3 5 5 3 4 3 0 6 1 5 7 5 1 4 2
Now it becomes very clear that sim's statements can be chained:
sim ← {B3 ← 1 1 0 ≠ B2 ← 1 0 1 ≠ B1 ← (3⍴2)⊤⍵ ⋄ C ← ¯3↑(-2⊥B2)↓⍺,B1 ⋄ 2⊥C≠B3}
We can even in-line C and eliminate B3, but that might also be a bit too much action:
sim ← {2⊥ (¯3↑(-2⊥B2)↓⍺,B1) ≠ 1 1 0 ≠ B2 ← 1 0 1 ≠ B1 ← (3⍴2)⊤⍵}
Here's the final version I might write:
sim ← {2⊥ (¯3↑(-2⊥B2)↓⍺,B1) ≠ 1 1 0 ≠ B2 ← 1 0 1 ≠ B1 ← (3⍴2)⊤⍵}
iter ← {⍬≡⍵: r,←⊂⍺ ⋄ a ← ⍸(⊃⍵)=⍺∘sim¨⍳8 ⋄ ⍬≡a: ⍬ ⋄ ⍺ ⍵∘((⊃⍤⊣,∘,(3⍴2)⊤⊢)∇ 1↓1⊃⊣)¨a}
r ← ⍬ ⋄ (7⍴0) iter 0 3 5 5 3 4 3 0 6 1 5 7 5 1 4 2
@Jack I hope this helps.
 
2 hours later…
18:13
@Adám Hi @Adám, I have been interested in APL for a while, but was learning BQN well enough before starting with another language :) I'm currently studying APL with Sergey's MOOC, and very likely will be asking questions here.
18:51
@panadestein Cool. Looking forward.
 
5 hours later…
23:58
i have two numeric arrays of the same shape, and i want to replace zeros in one with corresponding elements in the other; is (⊣+0∘=⍛×) the best way to do that?

« first day (2912 days earlier)      last day (36 days later) »