Skip to content

Instantly share code, notes, and snippets.

@Fflath
Last active December 6, 2022 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fflath/c5dd90d4b0601424d170697725b981e1 to your computer and use it in GitHub Desktop.
Save Fflath/c5dd90d4b0601424d170697725b981e1 to your computer and use it in GitHub Desktop.
AOC2022
⍝ Day 1
in1←,⎕csv'd1.txt'⍬4
⎕←⌈/+⌿⍤1⊢↑(≢¨in1)⊆in1
⎕←+/3↑(⊂∘⍒⌷⊢)+⌿⍤1⊢↑(≢¨in1)⊆in1
⍝ Day 2
in2←,/⎕csv⍠'Separator' ' '⊢'d2.txt'⍬4
C S←↓⍉9 2⍴'AX' 4 'BX' 1 'CX' 7 'AY' 8 'BY' 5 'CY' 2 'AZ' 3 'BZ' 9 'CZ' 6
⎕←+/S[C⍳in2]
C S←↓⍉9 2⍴'AX' 3 'AY' 4 'AZ' 8 'BX' 1 'BY' 5 'BZ' 9 'CX' 2 'CY' 6 'CZ' 7
⎕←+/S[C⍳in2]
⍝ Day 3
in3←⎕csv'd3.txt'⍬4
p←⍳52
chars←'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
⎕←+/p[chars⍳⊃¨(∩/)⍤1⊢{(⊃⍵)⊂⍨⊃(,/⊢,⊢)⊂1↑⍨2÷⍨⍴⊃⍵}⍤1⊢in3]
⎕←+/p[chars⍳⊃¨∩/⍤1⊢↑1↓⊂⍤⊢⌺(⍪3 3)⊢0 0,,in3]
⍝ Day 4
in4←↓⍤2⊢1000 2 2⍴{⎕csv⍠'Separator' '-'⊢⍵⍬4},⎕csv'd4.txt'⍬4
b ← {w1 w2←⍵⋄ (w1≤⍺)∧(⍺≤w2)}
between ← {a1 a2←⍺⋄w1 w2←⍵⋄ ((a1 b ⍵)∧(a2 b ⍵))∨((w1 b ⍺)∧(w2 b ⍺))}
overlap ← {a1 a2←⍺⋄w1 w2←⍵⋄ (a1 b ⍵)∨(a2 b ⍵)∨(w1 b ⍺)∨(w2 b ⍺)}
⎕←+/between/⍤1⊢in4
⎕←+/overlap/⍤1⊢in4
⍝ Day 5
⎕←initial_state←,/⍉{'[\[\]]'⎕R''⊢⍵}¨(17⍴1 0)/⎕csv⍠'Widths'(17⍴3 1)⊢(8↑,⎕csv'd5.txt'⍬4)''4
⎕←moves←↓0 1 0 1 0 1/⎕csv⍠'Separator' ' '⊢(10↓,⎕csv'd5.txt'⍬4)⍬4
]dinput
step←{
state moves ← ⍵
count from to ← ⊃moves
m←count↑from⊃state
state[from]←⊂count↓from⊃state
state[to]←⊂(⍺⍺ m),to⊃state
state (1↓moves)
}
⊃↓⍉↑⊃⌽step⍣501⊢ initial_state moves
⊃↓⍉↑⊃⊢step⍣501⊢ initial_state moves
⍝ Day 6
in6←⊃⎕csv'd6.txt'⍬4
3+4⍳⍨⊃,/1↓{⊂⍴∪⍵}⌺4⊢in6
13+14⍳⍨⊃,/6↓{⊂⍴∪⍵}⌺14⊢in6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment