/
a.vx
executable file
·58 lines (39 loc) · 881 Bytes
/
a.vx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env vx
const bindir = dirname realpath argv[ 0 ]
const input_path = if argc > 1 then {argv[ 1 ]} else {bindir "/input.txt"}
const data = (load input_path).lines()
var memory = [packed data / ','] map {int v.string}
var PC = 0
const opcodes = [ null, Math.sum, Math.product ]
def run (input)
{
while true do
{
const opcode = memory[ PC++ ]
const op = opcode % 100
var modes = opcode div 100
if op == 99 then
{
break
}
const i = memory[ PC++ ]
const a = if modes % 10 then {i} else {memory[ i ]}
if op == 3 then
{
memory[ i ] = input
continue
}
if op == 4 then
{
print^+(a != 0) a
continue
}
modes = modes div 10
const j = memory[ PC++ ]
const b = if modes % 10 then {j} else {memory[ j ]}
const k = memory[ PC++ ]
const c = opcodes[ op ]( a, b )
memory[ k ] = c
}
}
run 1