Last active
December 19, 2019 11:17
-
-
Save abrudz/eaedd2cc8af5a6c02283e2cd1260685d to your computer and use it in GitHub Desktop.
Solution to AoC 2015 P7
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
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