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}
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
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)
15:22
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
@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:
Btw, you can remove some redundant parens around
(2⊥B2)
and ,⍥⊂
and ,⍥∊
and (⍺,⍥⊂⍵)
is the same as ⍺ ⍵
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 ← 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
2 hours later…
5 hours later…
« first day (2912 days earlier) ← previous day next day → last day (36 days later) »