-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.vx
executable file
·106 lines (73 loc) · 1.49 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env vx
const bindir = dirname realpath argv[ 0 ]
const libdir = bindir "/../../lib/"
const import = { const path = libdir _ ".vsl"; eval( load path, path ) }
const permutations = (import "permute").permutations
const input_path = if argc > 1 then {argv[ 1 ]} else {bindir "/input.txt"}
const data = (load input_path).lines()[0]
const core = [data / ','] map int
def less (a, b) {a < b}
def equals (a, b) {a == b}
const opcodes = [ 0, Math.sum, Math.product, 3, 4, 5, 6, less, equals ]
def run (inputs)
{
var input = begin inputs
var memory = core
var PC = 0
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
{
return a
}
modes = modes div 10
const j = memory[ PC++ ]
const b = if modes % 10 then {j} else {memory[ j ]}
if op == 5 then
{
if a then
{
PC = b
}
continue
}
if op == 6 then
{
if not bool a then
{
PC = b
}
continue
}
const k = memory[ PC++ ]
const c = int opcodes[ op ]( a, b )
memory[ k ] = c
}
warn "not reached?"
}
def full_run (phases)
{
var input = 0
var p = begin phases
while p do
{
input = run [*p++, input]
}
return input
}
const outputs = permutations 5 map full_run
print Iter.max outputs