-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsol_05.aes
117 lines (108 loc) · 5.69 KB
/
sol_05.aes
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
107
108
109
110
111
112
113
114
115
116
117
@compiler >= 4.2.0
include "List.aes"
contract Day5 =
entrypoint solve_1() =
run(code(), [1])
entrypoint solve_2() =
run(code(), [5])
function run(code : list(int), input : list(int)) =
run'(0, setup_mem(code), input, [])
entrypoint run'(pc : int, mem : map(int, int), input : list(int), output : list(int)) =
let opcode = mem[pc]
let op = opcode mod 100
let i0 = (opcode / 100) mod 10
let i1 = (opcode / 1000) mod 10
switch(op)
99 => List.reverse(output)
1 => // Add
let x = val(pc + 1, i0, mem)
let y = val(pc + 2, i1, mem)
let z = mem[pc + 3]
run'(pc + 4, mem{[z] = x + y}, input, output)
2 => // Mul
let x = val(pc + 1, i0, mem)
let y = val(pc + 2, i1, mem)
let z = mem[pc + 3]
run'(pc + 4, mem{[z] = x * y}, input, output)
3 => // Input
let z = mem[pc + 1]
switch(input)
in :: input => run'(pc + 2, mem{[z] = in}, input, output)
[] => abort(String.concat("Program ran out of input at: ", Int.to_str(pc)))
4 => // Output
let x = val(pc + 1, i0, mem)
run'(pc + 2, mem, input, x :: output)
5 => // Jump if non-zero
let x = val(pc + 1, i0, mem)
let y = val(pc + 2, i1, mem)
run'(if(x != 0) y else pc + 3, mem, input, output)
6 => // Jump if zero
let x = val(pc + 1, i0, mem)
let y = val(pc + 2, i1, mem)
run'(if(x == 0) y else pc + 3, mem, input, output)
7 => // Less than
let x = val(pc + 1, i0, mem)
let y = val(pc + 2, i1, mem)
let z = mem[pc + 3]
run'(pc + 4, mem{[z] = if(x < y) 1 else 0}, input, output)
8 => // Equals than
let x = val(pc + 1, i0, mem)
let y = val(pc + 2, i1, mem)
let z = mem[pc + 3]
run'(pc + 4, mem{[z] = if(x == y) 1 else 0}, input, output)
_ =>
abort(String.concat("Bad opcode: ", Int.to_str(opcode)))
function val(ix : int, mode : int, mem : map(int, int)) =
if(mode == 0) mem[mem[ix]]
else mem[ix]
function setup_mem(is : list(int)) =
setup_mem'(0, is, {})
function
setup_mem' : (int, list(int), map(int, int)) => map(int, int)
setup_mem'(_, [], m) = m
setup_mem'(ix, x :: xs, m) = setup_mem'(ix + 1, xs, m{[ix] = x})
function code() =
[3, 225, 1, 225, 6, 6, 1100, 1, 238, 225, 104, 0, 1101, 90, 64, 225, 1101,
15, 56, 225, 1, 14, 153, 224, 101, -147, 224, 224, 4, 224, 1002, 223, 8,
223, 1001, 224, 3, 224, 1, 224, 223, 223, 2, 162, 188, 224, 101, -2014,
224, 224, 4, 224, 1002, 223, 8, 223, 101, 6, 224, 224, 1, 223, 224, 223,
1001, 18, 81, 224, 1001, 224, -137, 224, 4, 224, 1002, 223, 8, 223, 1001,
224, 3, 224, 1, 223, 224, 223, 1102, 16, 16, 224, 101, -256, 224, 224, 4,
224, 1002, 223, 8, 223, 1001, 224, 6, 224, 1, 223, 224, 223, 101, 48, 217,
224, 1001, 224, -125, 224, 4, 224, 1002, 223, 8, 223, 1001, 224, 3, 224,
1, 224, 223, 223, 1002, 158, 22, 224, 1001, 224, -1540, 224, 4, 224, 1002,
223, 8, 223, 101, 2, 224, 224, 1, 223, 224, 223, 1101, 83, 31, 225, 1101,
56, 70, 225, 1101, 13, 38, 225, 102, 36, 192, 224, 1001, 224, -3312, 224,
4, 224, 1002, 223, 8, 223, 1001, 224, 4, 224, 1, 224, 223, 223, 1102, 75,
53, 225, 1101, 14, 92, 225, 1101, 7, 66, 224, 101, -73, 224, 224, 4, 224,
102, 8, 223, 223, 101, 3, 224, 224, 1, 224, 223, 223, 1101, 77, 60, 225,
4, 223, 99, 0, 0, 0, 677, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1105, 0, 99999,
1105, 227, 247, 1105, 1, 99999, 1005, 227, 99999, 1005, 0, 256, 1105, 1,
99999, 1106, 227, 99999, 1106, 0, 265, 1105, 1, 99999, 1006, 0, 99999,
1006, 227, 274, 1105, 1, 99999, 1105, 1, 280, 1105, 1, 99999, 1, 225, 225,
225, 1101, 294, 0, 0, 105, 1, 0, 1105, 1, 99999, 1106, 0, 300, 1105, 1,
99999, 1, 225, 225, 225, 1101, 314, 0, 0, 106, 0, 0, 1105, 1, 99999, 7,
226, 677, 224, 1002, 223, 2, 223, 1005, 224, 329, 1001, 223, 1, 223, 1007,
226, 677, 224, 1002, 223, 2, 223, 1005, 224, 344, 101, 1, 223, 223, 108,
226, 226, 224, 1002, 223, 2, 223, 1006, 224, 359, 101, 1, 223, 223, 7,
226, 226, 224, 102, 2, 223, 223, 1005, 224, 374, 101, 1, 223, 223, 8, 677,
677, 224, 1002, 223, 2, 223, 1005, 224, 389, 1001, 223, 1, 223, 107, 677,
677, 224, 102, 2, 223, 223, 1006, 224, 404, 101, 1, 223, 223, 1107, 677,
226, 224, 102, 2, 223, 223, 1006, 224, 419, 1001, 223, 1, 223, 1008, 226,
226, 224, 1002, 223, 2, 223, 1005, 224, 434, 1001, 223, 1, 223, 7, 677,
226, 224, 102, 2, 223, 223, 1006, 224, 449, 1001, 223, 1, 223, 1107, 226,
226, 224, 1002, 223, 2, 223, 1005, 224, 464, 101, 1, 223, 223, 1108, 226,
677, 224, 102, 2, 223, 223, 1005, 224, 479, 101, 1, 223, 223, 1007, 677,
677, 224, 102, 2, 223, 223, 1006, 224, 494, 1001, 223, 1, 223, 1107, 226,
677, 224, 1002, 223, 2, 223, 1005, 224, 509, 101, 1, 223, 223, 1007, 226,
226, 224, 1002, 223, 2, 223, 1006, 224, 524, 101, 1, 223, 223, 107, 226,
226, 224, 1002, 223, 2, 223, 1005, 224, 539, 1001, 223, 1, 223, 1108, 677,
677, 224, 1002, 223, 2, 223, 1005, 224, 554, 101, 1, 223, 223, 1008, 677,
226, 224, 102, 2, 223, 223, 1006, 224, 569, 1001, 223, 1, 223, 8, 226,
677, 224, 102, 2, 223, 223, 1005, 224, 584, 1001, 223, 1, 223, 1008, 677,
677, 224, 1002, 223, 2, 223, 1006, 224, 599, 1001, 223, 1, 223, 108, 677,
677, 224, 102, 2, 223, 223, 1006, 224, 614, 1001, 223, 1, 223, 108, 226,
677, 224, 102, 2, 223, 223, 1005, 224, 629, 101, 1, 223, 223, 8, 677, 226,
224, 102, 2, 223, 223, 1005, 224, 644, 101, 1, 223, 223, 107, 677, 226,
224, 1002, 223, 2, 223, 1005, 224, 659, 101, 1, 223, 223, 1108, 677, 226,
224, 102, 2, 223, 223, 1005, 224, 674, 1001, 223, 1, 223, 4, 223, 99, 226]