Skip to content

Instantly share code, notes, and snippets.

@sampersand
Created February 4, 2023 09:21
Show Gist options
  • Save sampersand/4834b1412844005826a701efb529b1a3 to your computer and use it in GitHub Desktop.
Save sampersand/4834b1412844005826a701efb529b1a3 to your computer and use it in GitHub Desktop.
aoc 2019 day 2 part 1
CWD =: (<./CWD i:'/\') {.CWD=: ;(4!:3''){~4!:4 CWD=: <'CWD'
raw =: }: freads CWD , '/day2.txt'
die =: {{ echo y throw. }}
NB. raw =: '1,9,10,3,2,3,11,0,99,30,40,50'
ints =: ". every (',' cut raw)
ints =: (12 2) (1 2) } ints
number_of_params =: ((_ 3 3) , (_ * i.96) , 0) & {
clock_cycle =: {{
'ip memory' =. y
instruction =. ip { memory
params =. (ip + >: i. number_of_params instruction) { memory
ip =. ip + >: #params
select. instruction
case. 1 do. memory =. (+/ }:params { memory) ({:params) } memory
case. 2 do. memory =. (*/ }:params { memory) ({:params) } memory
case. 99 do. ip =. >:#memory
case. do. echo 'unknown opcode: ' , ":instruction NB. then somehow exit
end.
ip ; memory
}}
compare =: {{ a < #b [ 'a b' =. y }}
run =: {{ clock_cycle ^: compare ^:_ (0;y)}}
echo {.>{: run ints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment