Skip to content

Instantly share code, notes, and snippets.

@lynn

lynn/aoc17.apl Secret

Last active December 18, 2018 14:20
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 lynn/1dd09720a2d2e08a1602595c644c8827 to your computer and use it in GitHub Desktop.
Save lynn/1dd09720a2d2e08a1602595c644c8827 to your computer and use it in GitHub Desktop.
⎕io←0
'display' ⎕CY 'dfns'
⍝ I replaced newlines with ` because IDK how to deal with multi-line input other than files
input ← 'x=495, y=2..7`y=7, x=495..501`x=501, y=3..7`x=498, y=2..4`x=506, y=1..2`x=498, y=10..13`x=504, y=10..13`y=13, x=498..504'
⍝ Process it into a list of points:
split ← {⍵⊆⍨~⊃+/⌽∘(⍺⍷⍵)¨0-⍳≢⍺}
sort ← {⍵[⍋↑⍵]}
fromto ← {⍺↓⍳1+⍵}
parserange ← {⊃fromto/ ⍎¨ '..' split 2↓⍵}
points ← ↑,/↑,/(∘.,/¨ parserange¨∘⌽∘sort∘(', '∘split)¨ '`'split input)
⍝ Paint them on a grid:
s← 2014 700 ⍴ 0
s[points]←1
s[⊂0 500]←2
⍝ Water physics cellular automaton
rule ← {
(ul u ur l x r dl d dr)←,⍵
sl←(dl∊1 6)∧(l=3)
sr←(dr∊1 6)∧(r=3)
(⌈/,⍵)<2 :x
(x=0)∧(u>1) :2
(x=2)∧(d=1) :3
(x=0)∧(sl∨sr) :3
(x∊3 5)∧(l∊1 4) :x+1
(x∊3 4)∧(r∊1 5) :x+2
(x∊4 5)∧(6∊l r) :6
(x=2)∧(d=6) :3
x
}
simulate ← rule⌺(3 3)⍣≡
s← 0 450 ↓ s
⎕← {'.#|~⍨⍨≈'[⍵]}¨ (rule⌺(3 3)⍣0) s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment