Skip to content

Instantly share code, notes, and snippets.

@abrudz
Last active December 19, 2019 11:17
Show Gist options
  • Save abrudz/eaedd2cc8af5a6c02283e2cd1260685d to your computer and use it in GitHub Desktop.
Save abrudz/eaedd2cc8af5a6c02283e2cd1260685d to your computer and use it in GitHub Desktop.
Solution to AoC 2015 P7
P7S←{
⍝ Implement recursive topological sort
Segs←#.U.Segs
Trim←#.U.Trim
data←⍉⊃¨@2⍉' 'Segs¨Trim↑'->'∘Segs¨⍵
results←¯1⍴⍨≢data
ToBin←(16⍴2)∘⊤
FromBin←2∘⊥
NOT←=
AND←∧
OR←∨
SHIFT←{(⍺⍺ 16)↑⍺↓⍨FromBin ⍺⍺ ⍵}
LSHIFT←+SHIFT
RSHIFT←-SHIFT
Ö←{⊃⍺⍺/⍵⍵¨⍺ ⍵} ⍝ polyfill for ⍥
BIN←{FromBin ⍺ ⍺⍺ Ö ToBin ⍵}
Eval←{
vertex←⊃⌽⍵
∧/vertex∊⎕D:⍎vertex ⍝ If given expr is number, cast to number
value←⍺⊃results
0≤value:value ⍝ Value already found
(x f y)←(⊢,⍨'0⊢'↑⍨3-≢)⊃⍵
results[⍺]←x(⍎f)BIN Ö ∇ y
}
(⊢/data)results⊣(⍳∘≢Eval⍤1⊢)data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment