Last active
January 20, 2023 09:36
-
-
Save klao/b37b9a8f28c8235972ce94e8d6cc9f0e to your computer and use it in GitHub Desktop.
Advent of Code 2021, Dyalog APL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
⍝ https://adventofcode.com/2021 | |
⎕IO←1 ⍝ Except where it's explicitly changed | |
⍝ Day 01 | |
d←,⎕CSV'i/day01.inp'⍬2 | |
⎕← +/2</d | |
⎕← +/2</3+/d | |
⍝ Day 02 | |
forward←{⍵ 0} ⋄ down←{0 ⍵} ⋄ up←{0 (-⍵)} | |
d←⍉↑⍎¨⊃⎕NGET'i/day02.inp'1 | |
⎕← ×/+/d | |
⎕← (+/1⌷d)×1⌷d+.×+\2⌷d | |
⍝ Eval free solution: | |
d v←↓⍉⊃¨(⎕CSV⍠'Separator' ' ') 'i/day02t.inp'⍬4 | |
f←v×'f'=d | |
a←v×-⌿'du'∘.=d | |
⎕← (1⊥f)×1⊥a | |
⎕← (1⊥f)×f+.×+\a | |
⍝ Day 03 | |
d←'1'=↑⊃⎕NGET'i/day03.inp'1 | |
⎕← (2⊥~g)×2⊥g←(≢≤2×+⌿)d | |
filt←{i←0 ⋄ ⍺⍺{i+←1 ⋄ ⍵⌿⍨c=(≢ ⍺⍺ 2×+/)c←⍵[;i]}⍣{1=≢⍺}⍵} | |
⎕← ×/2⊥⍉(>filt⍪≤filt)d | |
⍝ Day 04 | |
d←⊃⎕NGET'i/day04.inp'1 | |
ns←⍎⊃d | |
bs←↑⍎¨↑(''∘≢¨⊆⊢)1↓d | |
m←∨⍀ns∘.=bs | |
ends←⍸0⍪2≠⌿5=⊃⌈/{⌈/+/[⍵]m}¨3 4 | |
score←{ns[⊃⍵]×1⊥,(⍵[2]⌷bs)×~⍵⌷m} | |
⎕← score ⊃ends | |
⎕← score ⊃⌽ends | |
⍝ Day 05 | |
⎕IO←0 | |
d←{2 2⍴⍎' '@(∊∘'->')⍵}¨⊃⎕NGET'i/day05.inp'1 | |
t←0⍴⍨1+⊃⌈/(⌈⌿)¨d | |
hv←(∨/=⌿)¨d | |
iot←{a b←⍵ ⋄ a>b:⌽b+⍳1+a-b ⋄ a+⍳1+b-a} | |
line←{(iot ⍵[;0]),¨(iot ⍵[;1])} | |
{t[line ⍵]+←1}¨hv/d | |
⎕← +/,2≤t | |
{t[line ⍵]+←1}¨(~hv)/d | |
⎕← +/,2≤t | |
⍝ Day 06 | |
⎕IO ⎕FR ⎕PP←0 1287 20 | |
d←,⎕CSV'i/day06.inp'⍬4 | |
80 256{+/{(⊃⍵)+@6⊢1⌽⍵}⍣⍺⊢⍵}¨⊂+⌿d∘.=⍳9 | |
⍝ Day 07 | |
⎕IO←0 | |
m←⌊2÷⍨≢d←,⎕CSV'i/day07.inp'⍬4 | |
⎕← 1⊥|d-(⊢⊃⍨m⊃⍋)d | |
⎕← 2÷⍨(⊃∘⍋⌷⊢)+⌿d∘.((⊢×1+⊢)|⍤-)⍳⌈/d | |
⍝ Part 2, non brute-force: | |
)COPY dfns bsearch | |
⎕← 2÷⍨1⊥(⊢×1+⊢)|d-{0≤1⊥(⊢+0≤⊢)⍵-d}bsearch (⌊/,⌈/)d | |
⍝ Day 08 | |
⎕IO←0 | |
d←(~⍤∊∘' |'⊆⊢)¨⊃⎕NGET'i/day08.inp'1 | |
⎕← 1⊥,2 3 4 7∊⍨≢¨10↓⍉↑d | |
dec←{x←10↑⍵ ⋄ c←x⌷⍨⊂(⊂7 0 3 5 2 4 6 1 9 8)⌷⍋+/x∘.(+.×)x ⋄ ⊃10⊥⍸⍤1⍉c∘.≡10↓⍵} | |
⎕← 1⊥(dec (⊂'abcdefg')∊¨⊢)¨d | |
⍝ Day 09 | |
⎕IO←0 | |
d←1+⍎¨↑⊃⎕NGET'i/day09.inp'1 | |
ns←(⊂1 3 5 7)⌷, ⋄ m←⊃1 1⌷⊢ | |
⎕← 1⊥,d×{(m⍵)<⌊/ns⍵}⌺3 3⊢d-10 | |
⎕← ×/((⊂3↑⍒)⌷⊢) {≢⍵}⌸1↓¨, ({c←⊃m⍵ ⋄ c>9:⊂m⍵ ⋄ d←∪1↓¨n/⍨(0∘≠∧c∘>)⊃¨n←ns⍵ ⋄ 1=≢d:⊂c,⊃d ⋄ ⊂m⍵}⌺3 3)⍣≡ d,¨⍳⍴d | |
⍝ Day 10 | |
d←(¯5+'<{[( )]}>'⍳⊢)¨⊃⎕NGET'i/day10.inp'1 | |
p←{⊃{0=≡⍵:⍵ ⋄ ⍺<0:⍺,⍵ ⋄ ⍺=-⊃⍵:1↓⍵ ⋄ ⍺}/(⌽⍵),⊂⍬}¨d | |
⎕← +/3 57 1197 25137⌷⍨⊂p/⍨~≡¨p | |
⎕← {⍵⌷⍨(⍋⍵)⌷⍨(1+≢⍵)÷2}5∘⊥¨-p/⍨≡¨p | |
⍝ Day 11 | |
d←⍎¨↑⊃⎕NGET'i/day11.inp'1 | |
step←{(9∘≥×⊢){⍺+{1⊥9<,⍵}⌺3 3⊢⍵}⍣≡⍨1+⍵} | |
s ⊣(⊢⊣{s+←1⊥0=,⍵})⍤step⍣100⊢d ⊣s←0 | |
i ⊣step⍣{i+←1 ⋄ 0=1⊥,⍺}d ⊣i←0 | |
⍝ Day 12 | |
d←('-'∘≠⊆⊢)¨⊃⎕NGET'i/day12.inp'1 | |
ns←∪⊃,/d ⋄ st en←ns⍳'start' 'end' | |
low←(⊃⎕C=⊢)¨ns ⋄ adj←(⍉∨⊢)ns∘.{d∊⍨⊂(⍺ ⍵)}ns | |
paths←{⍵=0:0 ⋄ ⍵=en:1 ⋄ v←low×(1@⍵)⍺ ⋄ +/(⊂v)∇¨⍸(~v)×⍵⌷adj} | |
⎕← (0×low) paths st | |
paths2←{⍵=0:0 ⋄ ⍵=en:1 ⋄ v←low×1(+@⍵)⍺ ⋄ 2<1⊥2=v:0 ⋄ +/(⊂v)∇¨⍸(v<2)×⍵⌷adj} | |
⎕← (1@st⊢0×low)paths2 st | |
⍝ Day 13 | |
ds0 is0←(''∘≢¨⊆⊢)⊃⎕NGET'i/day13.inp'1 | |
ds←⍎¨ds0 ⋄ is←{d p←'='(≠⊆⊢)11↓⍵ ⋄ ('xy'⍳d)(⍎p)}¨is0 | |
flip←{d p←⍺⍺ ⋄ p<d⌷⍵:p(⊢-⍨2×⊣)@d⊢⍵ ⋄ ⍵} | |
⎕← ≢∪(⊃is)flip¨ds | |
⎕← '#'@(⌽¨⊃{⍺flip¨⍵}/(⌽is),⊂ds)⊢10 40⍴' ' | |
⍝ Day 14 | |
⎕IO ⎕FR ⎕PP←1 1287 30 | |
d←⊃⎕NGET'i/day14.inp'1 | |
gs gl gr←↓⍉↑{l r m←⍵[1 2 7] ⋄ (l r)(l m)(m r)}¨2↓d | |
⍝ assert (gl,gr)∊gs ⍝ transitions sanity check | |
in←↑2{gs≡¨⊂⍺⍵}/⊃d | |
⍝ assert +/in ⍝ input sanity check | |
m←(gs∘.≡gl)+(gs∘.≡gr) ⍝ transition matrix | |
⍝ assume first and last letter not the same, otherwise might have an off-by-one: | |
⎕← {⌈2÷⍨(⌈/-⌊/)(m+.×⍣⍵+⌿in)+.×(gs∘.(+/=)∪∊gs)}¨10 40 | |
⍝ Day 15 | |
d←↑⍎¨¨⊃⎕NGET'i/day15t.inp'1 | |
sp←{⊃⊖⌽⍵{⍵⌊⍺+⌊⌿m{↑(2⊢/⍵,⍺)(2⊣/⍺,⍵)(2⊢⌿⍵⍪⍺)(2⊣⌿⍺⍪⍵)}⍵}⍣≡0@(⊂1 1)⊢(⍴⍵)⍴m←9×+/⍴⍵} | |
⎕← sp d | |
⎕← sp 1+9|1-⍨(⊣⍪1+⊢)⍣4⍨(⊣,1+⊢)⍣4⍨d | |
⍝ Day 16 | |
⎕PP←30 | |
)COPY dfns dec | |
unh←∊((4/2)⊤dec)¨ | |
d←unh⊃⊃⎕NGET'i/day16.inp'1 | |
num←{l←⊃⍸~1⌷⍉d←(⌊5÷⍨≢⍵)5⍴⍵ ⋄ (2⊥,⍉1↓⍉l↑d)(⍵↓⍨5×l)} | |
parse←{v←2⊥3↑⍵ ⋄ t←2⊥3↑3↓⍵ ⋄ c r←t parsec 6↓⍵ ⋄ (1=⍺)∨0=≢r:(⊂v t c)r ⋄ m r←(⍺-1)∇ r ⋄ (m,⍨⊂v t c)r} | |
parsec←{4=⍺:num ⍵ ⋄ b←1↓⍵ ⋄ ⊃⍵:(2⊥11↑b)parse 11↓b ⋄ l←2⊥15↑b ⋄ ps _←¯1 parse l↑15↓b ⋄ ps(l↓15↓b)} | |
versum←{v t c←⍵ ⋄ 4=t:v ⋄ v+1⊥∇¨c} | |
⎕← versum⊃⊃1 parse d | |
eval←{v t c←⍵ ⋄ 4=t:c ⋄ s←∇¨c ⋄ 0=t:+/s ⋄ 1=t:×/s ⋄ 2=t:⌊/s ⋄ 3=t:⌈/s ⋄ 5=t:>/s ⋄ 6=t:</s ⋄ 7=t:=/s} | |
⎕← eval⊃⊃1 parse d | |
⍝ Day 17 | |
xl xh yl yh←⍎¨('-',⎕D)(∊⍨⊆⊢)⊃⊃⎕NGET'i/day17.inp'1 | |
⎕← 2!-yl | |
s←2×1-yl | |
hit←{∨/((xh∘≥∧xl∘≤)+\0⌈1+(s/⍺)-⍳s)∧(yh∘≥∧yl∘≤)+\1+(s/⍵)-⍳s} | |
⎕← 1⊥,(⍳xh)∘.hit(¯1+yl+⍳s-yl) | |
⍝ Day 18 | |
assert←{⍺←'assertion failure' ⋄ 0∊⍵:⍺ ⎕signal 8 ⋄ shy←0} | |
d←(⍎{'() '⌷⍨⊂'[],'⍳⍵}@(∊∘'[],'))¨⊃⎕NGET'i/day18.inp'1 | |
lin←{⍺←64 ⋄ 0=⍴⍴⍵:⍵+¯11○⍺ ⋄ ∊(⍺÷2)∘∇¨⍵} | |
delin←{1=≢⍵:9○⊃⍵ ⋄ s←{⊃⍸(2÷⍨+/⍵)=+\⍵}11○⍵ ⋄ (∇ s↑⍵)(∇ s↓⍵)} | |
split←{∊{⊂(⌊,⌈)⍵÷2}@(⊃⍸10≤9○⍵)⊢⍵} | |
explode←{f←⊃⍸2=11○⍵ ⋄ a b←⍵[f(f+1)] ⋄ ∊(⊂⍬)@(f+1)⊢0J4@f{f>1:{⍵+9○a}@(f-1)⊢⍵ ⋄ ⍵}{(f+2)≤≢⍵:{⍵+9○b}@(f+2)⊢⍵ ⋄ ⍵}⍵} | |
reduce←{_←assert 2≤11○⍵ ⋄ 2∊11○⍵:explode ⍵ ⋄ 1∊10≤9○⍵:split ⍵ ⋄ ⍵} | |
add←{reduce⍣≡{⍵-¯11○2÷⍨11○⍵}⍺,⍵} | |
mag←{0=⍴⍴⍵:⍵ ⋄ +/3 2×∇¨⍵} | |
⎕← mag delin ⊃add⍨/⌽lin¨d | |
⎕← ⌈/,∘.{mag delin ⍺ add ⍵}⍨lin¨d | |
⍝ Day 19 | |
d←{⎕CSV(1↓⍵)⍬2}¨(''∘≢¨⊆⊢)⊃⎕NGET'i/day19.inp'1 | |
det3←{+/⊃-/×⌿¨(⍵⌽⍨⍳3)(⍵⌽⍨-⍳3)} | |
rots←{⍵/⍨(1=det3¨⍵)}{↑↑{⊂⍵⌷(⊢⍪-)∘.=⍨⍳3}¨⍵}¨(,⍳ 6 6 6) | |
line2←{ | |
x y←⍺ ⍵ | |
(↓x)∘.{≢(↓x-⍤1⊢⍺)∩(↓y-⍤1⊢⍵)}↓y | |
} | |
match2←{ | |
x y←⍺ ⍵ | |
m←⍸12≤↑{x line2 y+.×⍵}¨rots | |
0=≢m:0 ⍬ | |
r xi yi←⊃m | |
yr←y+.×⊃r⌷rots | |
1(yr-⍤1⊢(yi⌷yr)-xi⌷x) | |
} | |
match←{ | |
s←⍵ | |
m←(≢s)/0 | |
f←m | |
m[1]←1 | |
{ | |
×/m:s | |
xi←⊃⍸m∧~f | |
x←⊃xi⌷s | |
_←{r←x match2⊃⍵⌷s ⋄ 0=≢r:0 ⋄ s[⍵]←⊂r ⋄ m[⍵]←1}¨⍸~m | |
f[xi]←1 | |
∇ 0 | |
}0 | |
} | |
z zv←match d | |
≢⊃∪/↓¨z | |
⌈/,∘.{1⊥|⍺-⍵}⍨zv | |
⍝ Day 20 | |
⎕IO←0 | |
c i←{(⊃⍵)(↑2↓⍵)}'#'=⊃⎕NGET'i/day20.inp'1 | |
{⎕← 1⊥,{⍉⌽n↓⍵}⍣4{c⌷⍨⊂2⊥,⍵}⌺3 3⍣n{⍉⌽0,⍵}⍣(8×n←⍵)⊢i}¨2 50 | |
⍝ Day 21 | |
⎕IO ⎕FR ⎕PP←1 1287 30 | |
d←{⍎⊃⌽' '(≠⊆⊢)⍵}¨⊃⎕NGET'i/day21.inp'1 | |
r p←⊃⍸1000≤s←⍉+\1+10|↑(d-1)+↓+\⍉¯3+9×1000 2⍴⍳2000 | |
⎕← (3×p+2×r-1)×((,s)⌷⍨¯3+p+2×r) | |
t←↓{⍺(≢⍵)}⌸,∘.+⍣2⍨⍳3 | |
tr←{n←0⍴⍨⍴⍵ ⋄ 0∧.=,21↑⍵:n ⋄ _←t∘.{d m←⍺ ⋄ s p k←⍵ ⋄ pp←1+10|p+d-1 ⋄ n[s+pp;pp]+←m×k}{⍵,⍵⌷o}¨⍸0≠21↑o←⍵ ⋄ n} | |
go←{r←32 0⍴0 ⋄ _←{r,←+/⍵ ⋄ tr ⍵}⍣13⊢1@(⊂1 ⍵)⊢32 10⍴0 ⋄ (+⌿21↓r)(+⌿21↑r)} | |
fw fc←go d[1] | |
sw sc←go d[2] | |
⎕← (sw+.×fc)⌈(fw+.ׯ1⌽sc) | |
⍝ Day 22 | |
⎕IO ⎕PP←0 30 | |
d←{('on'∘≡¨⊣/⍵),¨↓(⊂0 1)+↓50+(≢⍵)3 2⍴⍎¨2⊢/⍵}↑{⍵⊆⍨~⍵∊' xyz=.,'}¨⊃⎕NGET'i/day22.inp'1 | |
sm←{∧/,(0∘≤∧101∘≥)↑⍵}¨d | |
ig←{(⊃⊃⍵)+⍳-⍨/∊⍵} | |
go←{t←0⍴⍨3/l←⍺ ⋄ _←{t[ig 1⌷⍵;ig 2⌷⍵;ig 3⌷⍵]←⊃⍵}¨⍵ ⋄ t} | |
⎕← p1←+/,101go sm/d | |
m←⍉↑(~sm)/d ⋄ is←↑{b←(⊂∘⍋⌷⊢)∪∊l←m⌷⍨⍵+1 ⋄ m[⍵+1;]←↓b⍳↑l ⋄ ¯2-/b}¨⍳3 | |
⍝ Necessary to save memory: | |
⎕← p1+(0⌷is)+.×{+/∊(1⌷is)×[0](2⌷is)×[1]r[⍵;;]}¨⍳⊃⍴r←(≢⍉is)go↓⍉m | |
⍝ Day 23 | |
⍝ Solved by hand :) | |
⍝ Day 24 | |
⍝ Solved by hand :) | |
⍝ Day 25 | |
d←('>'∘=+2×'v'∘=)↑⊃⎕NGET'i/day25.inp'1 | |
move←{h←(¯1∘⌽+⍵×~)(1=⍵)∧0=1⌽⍵ ⋄ (2ׯ1⊖d)+h×~d←(2=h)∧0=1⊖h} | |
⎕← i⊣move⍣{⍺≡⍵⊣i+←1}d⊣i←0 |
Using { ⊃⌽⊖⍵} instead of {⊢/⊢/⍵}... why?
Not sure! I was just learning APL. Maybe I haven't encountered that construct at that point, maybe I found it interesting for some other reason… 🤷
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using { ⊃⌽⊖⍵} instead of {⊢/⊢/⍵}... why?