Skip to content

Instantly share code, notes, and snippets.

@codesections
Last active December 6, 2019 04:13
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 codesections/68e73bf832061a6cd804a4e02c653dcf to your computer and use it in GitHub Desktop.
Save codesections/68e73bf832061a6cd804a4e02c653dcf to your computer and use it in GitHub Desktop.
Advent of Code day 5
⎕IO←0
intcode←{
⍺←0 ⋄ i←⍺ ⋄ s←⍵ ⋄ instr←'0'(,⍣{4<≢⍺})⍕⍺⌷⍵ ⋄ opcode←⍎¯2↑instr ⋄ p_modes←⍎¨⌽3⍴instr
p←{mode←p_modes[⍵-1]
mode=0:s[i+⍵]
mode=1:i+⍵
'ERR: Unknown parameter mode',mode}
i≥≢s:'ERR: no HALT opcode'
1=opcode:{s[p 3]←s[p 1]+s[p 2] ⋄ (i+4) intcode s}⍵
2=opcode:{s[p 3]←s[p 1]×s[p 2] ⋄ (i+4) intcode s}⍵
3=opcode:{⍞←' IN: ' ⋄ s[p 1]←⍎5↓⍞ ⋄ (i+2) intcode s}⍵
4=opcode:{⎕←'OUT:',s[p 1] ⋄ (i+2) intcode s}⍵
5=opcode:{0≠s[p 1]: s[p 2] intcode s
(i+3) intcode s}⍵
6=opcode:{0=s[p 1]: s[p 2] intcode s
(i+3) intcode s}⍵
7=opcode:{s[p 1]<s[p 2]:{s[p 3]←1 ⋄ (i+4) intcode s}⍵
{s[p 3]←0 ⋄ (i+4) intcode s}⍵ }⍵
8=opcode:{s[p 1]=s[p 2]:{s[p 3]←1 ⋄ (i+4) intcode s}⍵
{s[p 3]←0 ⋄ (i+4) intcode s}⍵ }⍵
99=opcode:''
'ERR: Unknown opcode',opcode
}
in←⍎¨','(≠⊆⊢) ⊃⊃⎕nget '05.input' 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment