Skip to content

Instantly share code, notes, and snippets.

@xpqz
Last active December 14, 2020 12:43
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 xpqz/c80e65a469a01fac55e20472e80f3b5c to your computer and use it in GitHub Desktop.
Save xpqz/c80e65a469a01fac55e20472e80f3b5c to your computer and use it in GitHub Desktop.
Advent of Code 2020 Day14
DAY14←{('10X'=⊂7↓0⊃⍵)({⍎⍕'\d+'⎕S'&'⊢⍵}¨1↓⍵)}¨({'mask'≡4↑⍵}¨d)⊂d←⊃⎕NGET'data/2020/day14.txt'1
mem←65435/0
_←{(0⊃⍵)∘{mem[0⊃⍵]←2⊥(((36⍴2)⊤1⊃⍵)∨0⊃⍺)∧~1⊃⍺⋄⍬}¨1⊃⍵}¨DAY14
+/mem ⍝ Part 1
mem←⍬ ⍝ Need the whole 36-bit address space, but sparsely populated
val←⍬
SetMem←{i←mem⍳⍵⋄i<≢mem:⍬⊣val[i]←⍺⋄mem,←⍵ ⋄ val,←⍺⋄⍬}
Addr←{
b←((36⍴2)⊤0⊃⍵)∨0⊃⍺ ⍝ Address in binary, with set-bit mask applied
bits←{⍉(⍵⍴2)⊤⍳2*⍵} +/flb←2⊃⍺
all←(≢bits)(≢b)⍴b
all[;⍸flb]←bits ⍝ 'all' now contains all expanded floating bits
_←(1⊃⍵)∘SetMem¨2⊥¨↓all
}
_←{(0⊃⍵)∘Addr¨1⊃⍵}¨DAY14
+/val ⍝ Part 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment