-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.zig
182 lines (163 loc) · 8.52 KB
/
9.zig
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const IntComputer = struct {
ip: usize = 0,
mem: []i64,
in: i64 = 0,
result: i64 = 0,
base: i64 = 0,
hlt: bool = false,
allocator: *std.mem.Allocator,
fn init(allocator: *std.mem.Allocator, memsize: usize) !IntComputer {
return IntComputer{
.allocator = allocator,
.mem = try allocator.alloc(i64, memsize),
};
}
fn deinit(ic: *IntComputer) void {
ic.allocator.free(ic.mem);
}
fn load(ic: *IntComputer, prog: []const i64) void {
std.mem.copy(i64, ic.mem, prog);
std.mem.set(i64, ic.mem[prog.len..], 0);
ic.hlt = false;
}
fn getVal(ic: *IntComputer, argIndex: u8) *i64 {
const n: i64 = switch (argIndex) {
1 => 100,
2 => 1000,
3 => 10000,
else => unreachable,
};
const arg = ic.mem[ic.ip + argIndex];
const mode = @rem(@divTrunc(ic.mem[ic.ip], n), 10);
return switch (mode) {
0 => &ic.mem[@intCast(usize, arg)],
1 => &ic.mem[ic.ip + argIndex],
2 => &ic.mem[@intCast(usize, ic.base + arg)],
else => unreachable,
};
}
fn op1(ic: *IntComputer) void {
var val1 = ic.getVal(1).*;
var val2 = ic.getVal(2).*;
var res = ic.getVal(3);
res.* = val1 + val2;
ic.ip += 4;
}
fn op2(ic: *IntComputer) void {
var val1 = ic.getVal(1).*;
var val2 = ic.getVal(2).*;
var res = ic.getVal(3);
res.* = val1 * val2;
ic.ip += 4;
}
fn op3(ic: *IntComputer) void {
var val1 = ic.getVal(1);
val1.* = ic.in;
ic.ip += 2;
}
fn op4(ic: *IntComputer) void {
var val1 = ic.getVal(1).*;
ic.result = val1;
ic.ip += 2;
}
fn op5(ic: *IntComputer) void {
var val1 = ic.getVal(1).*;
var val2 = ic.getVal(2).*;
if (val1 != 0)
ic.ip = @intCast(usize, val2)
else
ic.ip += 3;
}
fn op6(ic: *IntComputer) void {
var val1 = ic.getVal(1).*;
var val2 = ic.getVal(2).*;
if (val1 == 0)
ic.ip = @intCast(usize, val2)
else
ic.ip += 3;
}
fn op7(ic: *IntComputer) void {
var val1 = ic.getVal(1).*;
var val2 = ic.getVal(2).*;
var res = ic.getVal(3);
if (val1 < val2)
res.* = 1
else
res.* = 0;
ic.ip += 4;
}
fn op8(ic: *IntComputer) void {
var val1 = ic.getVal(1).*;
var val2 = ic.getVal(2).*;
var res = ic.getVal(3);
if (val1 == val2)
res.* = 1
else
res.* = 0;
ic.ip += 4;
}
fn op9(ic: *IntComputer) void {
var val1 = ic.getVal(1).*;
ic.base += val1;
ic.ip += 2;
}
fn run(ic: *IntComputer) void {
var asked = true;
while (true) {
switch (@rem(ic.mem[ic.ip], 100)) {
1 => ic.op1(),
2 => ic.op2(),
3 => if (asked) {
ic.op3();
asked = false;
} else {
return;
},
4 => {
ic.op4();
return;
},
5 => ic.op5(),
6 => ic.op6(),
7 => ic.op7(),
8 => ic.op8(),
9 => ic.op9(),
99 => {
ic.hlt = true;
return;
},
else => unreachable,
}
}
}
};
const std = @import("std");
const program = [_]i64{ 1102, 34463338, 34463338, 63, 1007, 63, 34463338, 63, 1005, 63, 53, 1101, 0, 3, 1000, 109, 988, 209, 12, 9, 1000, 209, 6, 209, 3, 203, 0, 1008, 1000, 1, 63, 1005, 63, 65, 1008, 1000, 2, 63, 1005, 63, 904, 1008, 1000, 0, 63, 1005, 63, 58, 4, 25, 104, 0, 99, 4, 0, 104, 0, 99, 4, 17, 104, 0, 99, 0, 0, 1101, 0, 252, 1023, 1101, 0, 0, 1020, 1102, 1, 39, 1013, 1102, 1, 234, 1029, 1102, 26, 1, 1016, 1101, 37, 0, 1005, 1101, 0, 27, 1011, 1101, 21, 0, 1000, 1101, 0, 29, 1019, 1101, 35, 0, 1003, 1102, 22, 1, 1007, 1102, 1, 32, 1001, 1101, 1, 0, 1021, 1102, 1, 216, 1027, 1102, 30, 1, 1012, 1102, 1, 24, 1009, 1101, 36, 0, 1002, 1101, 0, 31, 1010, 1101, 0, 243, 1028, 1102, 787, 1, 1024, 1102, 255, 1, 1022, 1102, 33, 1, 1017, 1102, 1, 23, 1004, 1102, 778, 1, 1025, 1102, 1, 28, 1008, 1101, 0, 223, 1026, 1102, 1, 25, 1015, 1101, 0, 20, 1006, 1102, 34, 1, 1014, 1101, 38, 0, 1018, 109, -4, 1202, 5, 1, 63, 1008, 63, 32, 63, 1005, 63, 203, 4, 187, 1106, 0, 207, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, 37, 2106, 0, -6, 1001, 64, 1, 64, 1106, 0, 225, 4, 213, 1002, 64, 2, 64, 109, 3, 2106, 0, -8, 4, 231, 1001, 64, 1, 64, 1105, 1, 243, 1002, 64, 2, 64, 109, -12, 2105, 1, -1, 1105, 1, 261, 4, 249, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, -13, 2102, 1, -3, 63, 1008, 63, 31, 63, 1005, 63, 285, 1001, 64, 1, 64, 1106, 0, 287, 4, 267, 1002, 64, 2, 64, 109, 6, 21102, 40, 1, 0, 1008, 1017, 40, 63, 1005, 63, 313, 4, 293, 1001, 64, 1, 64, 1105, 1, 313, 1002, 64, 2, 64, 109, -10, 2107, 31, -6, 63, 1005, 63, 331, 4, 319, 1105, 1, 335, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, -6, 2102, 1, 7, 63, 1008, 63, 28, 63, 1005, 63, 357, 4, 341, 1105, 1, 361, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, 2, 21107, 41, 40, 8, 1005, 1011, 377, 1106, 0, 383, 4, 367, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, -1, 1201, 2, 0, 63, 1008, 63, 26, 63, 1005, 63, 403, 1106, 0, 409, 4, 389, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, 22, 1205, -4, 425, 1001, 64, 1, 64, 1105, 1, 427, 4, 415, 1002, 64, 2, 64, 109, -9, 21101, 42, 0, 3, 1008, 1018, 39, 63, 1005, 63, 451, 1001, 64, 1, 64, 1105, 1, 453, 4, 433, 1002, 64, 2, 64, 109, 3, 21107, 43, 44, 0, 1005, 1018, 475, 4, 459, 1001, 64, 1, 64, 1105, 1, 475, 1002, 64, 2, 64, 109, -7, 21101, 44, 0, 0, 1008, 1011, 44, 63, 1005, 63, 497, 4, 481, 1105, 1, 501, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, 17, 1206, -7, 513, 1105, 1, 519, 4, 507, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, -24, 1207, 5, 25, 63, 1005, 63, 537, 4, 525, 1105, 1, 541, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, 7, 21108, 45, 43, 2, 1005, 1013, 557, 1106, 0, 563, 4, 547, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, -5, 1207, -3, 34, 63, 1005, 63, 583, 1001, 64, 1, 64, 1106, 0, 585, 4, 569, 1002, 64, 2, 64, 109, 5, 21108, 46, 46, 5, 1005, 1016, 607, 4, 591, 1001, 64, 1, 64, 1105, 1, 607, 1002, 64, 2, 64, 109, -12, 2108, 20, 8, 63, 1005, 63, 627, 1001, 64, 1, 64, 1105, 1, 629, 4, 613, 1002, 64, 2, 64, 109, 24, 1206, -3, 647, 4, 635, 1001, 64, 1, 64, 1105, 1, 647, 1002, 64, 2, 64, 109, -30, 2108, 32, 8, 63, 1005, 63, 665, 4, 653, 1106, 0, 669, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, 22, 1208, -9, 20, 63, 1005, 63, 691, 4, 675, 1001, 64, 1, 64, 1106, 0, 691, 1002, 64, 2, 64, 109, -4, 21102, 47, 1, 3, 1008, 1014, 49, 63, 1005, 63, 715, 1001, 64, 1, 64, 1105, 1, 717, 4, 697, 1002, 64, 2, 64, 109, -10, 2101, 0, 1, 63, 1008, 63, 36, 63, 1005, 63, 743, 4, 723, 1001, 64, 1, 64, 1105, 1, 743, 1002, 64, 2, 64, 109, 16, 1201, -9, 0, 63, 1008, 63, 28, 63, 1005, 63, 769, 4, 749, 1001, 64, 1, 64, 1105, 1, 769, 1002, 64, 2, 64, 109, 2, 2105, 1, 5, 4, 775, 1001, 64, 1, 64, 1106, 0, 787, 1002, 64, 2, 64, 109, -5, 1202, -6, 1, 63, 1008, 63, 26, 63, 1005, 63, 807, 1106, 0, 813, 4, 793, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, -16, 2107, 37, 4, 63, 1005, 63, 833, 1001, 64, 1, 64, 1105, 1, 835, 4, 819, 1002, 64, 2, 64, 109, 2, 2101, 0, 1, 63, 1008, 63, 34, 63, 1005, 63, 855, 1105, 1, 861, 4, 841, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, 19, 1205, 2, 875, 4, 867, 1105, 1, 879, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, -2, 1208, -8, 23, 63, 1005, 63, 899, 1001, 64, 1, 64, 1106, 0, 901, 4, 885, 4, 64, 99, 21101, 0, 27, 1, 21102, 915, 1, 0, 1106, 0, 922, 21201, 1, 61455, 1, 204, 1, 99, 109, 3, 1207, -2, 3, 63, 1005, 63, 964, 21201, -2, -1, 1, 21102, 942, 1, 0, 1105, 1, 922, 22102, 1, 1, -1, 21201, -2, -3, 1, 21102, 1, 957, 0, 1105, 1, 922, 22201, 1, -1, -2, 1106, 0, 968, 22101, 0, -2, -2, 109, -3, 2105, 1, 0 };
fn puzzle1() !void {
var ic = try IntComputer.init(std.heap.page_allocator, 5 * 1024);
defer ic.deinit();
ic.load(&program);
ic.in = 1;
while (true) {
ic.run();
if (ic.hlt)
return;
std.debug.warn("{}\n", ic.result);
}
}
fn puzzle2() !void {
var ic = try IntComputer.init(std.heap.page_allocator, 5 * 1024);
defer ic.deinit();
ic.load(&program);
ic.in = 2;
while (true) {
ic.run();
if (ic.hlt)
return;
std.debug.warn("{}\n", ic.result);
}
}
pub fn main() !void {
try puzzle1();
try puzzle2();
}