Created
December 10, 2019 00:36
-
-
Save codesections/e08110f6392783cc7e8c130e111b4f44 to your computer and use it in GitHub Desktop.
Advent of Code day 9
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
intcode←{ | |
⍺←0 0 ⋄ (i rb)←⍺ ⋄ val←⊃⍵[0] ⋄ addr←⊃⍵[1] | |
get←{ ⍵<0: ⎕←'ERR: invalid negative index' | |
(addr⍳⍵)<(≢addr):(addr⍳⍵)⌷val | |
addr,←⍵ ⋄ val,←0 ⋄ ∇ ⍵ } | |
set←{ (addr⍳⍺)<(≢addr):((addr⍳⍺)⌷val)←⍵ | |
addr,←⍺ ⋄ val,←⍵ } | |
instr←(10 10 10 10 10)⊤(get i) ⋄ opcode←(10 10)⊥¯2↑instr ⋄ p_modes←⌽3↑instr | |
p←{mode←p_modes[⍵-1] | |
mode=0:get i+⍵ ⍝ positional | |
mode=1:i+⍵ ⍝ immediate | |
mode=2:(get i+⍵)+rb ⍝ relative base | |
'ERR: Unknown parameter mode',mode} | |
i≥≢val:'ERR: no HALT opcode' | |
1=opcode:{_←(p 3) set ((get p 1)+(get p 2)) ⋄ ((i+4) rb) intcode val addr}⍵ | |
2=opcode:{_←(p 3) set ((get p 1)×(get p 2)) ⋄ ((i+4) rb) intcode val addr}⍵ | |
3=opcode:{_←(p 1) set ⍎⍞ ⋄ ((i+2) rb) intcode val addr}⍵ | |
4=opcode:{⎕←(get p 1) ⋄ ((i+2) rb) intcode val addr}⍵ | |
5=opcode:{0≠(get p 1): ((get p 2) rb) intcode val addr | |
((i+3) rb) intcode val addr}⍵ | |
6=opcode:{0=(get p 1): ((get p 2) rb) intcode val addr | |
((i+3) rb) intcode val addr}⍵ | |
7=opcode:{(get p 1)<(get p 2):{_←(p 3) set 1 ⋄ ((i+4) rb) intcode val addr}⍵ | |
{_←(p 3) set 0 ⋄ ((i+4) rb) intcode val addr}⍵ }⍵ | |
8=opcode:{(get p 1)=(get p 2):{_←(p 3) set 1 ⋄ ((i+4) rb) intcode val addr}⍵ | |
{_←(p 3) set 0 ⋄ ((i+4) rb) intcode val addr}⍵ }⍵ | |
9=opcode:{rb+←(get p 1) ⋄ ((i+2) rb) intcode val addr}⍵ | |
99=opcode:'' | |
'ERR: Unknown opcode',opcode | |
} | |
in←⍎¨','(≠⊆⊢) ⊃⊃⎕nget '09.input' 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment