-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintCode.ts
255 lines (238 loc) · 5.7 KB
/
intCode.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
interface Wire {
value: number;
}
export const createWire = (initial: number): Wire => {
let _value = initial;
return {
set value(e) {
_value = e;
return;
},
get value() {
return _value;
}
};
};
export type Machine = {
memory: number[];
input: number | Wire;
output: number | Wire;
cursor: number;
mode: number;
relative: number;
setMode: (mode: number) => void;
setRelative: (rel: number) => Machine;
next(mode?: 0 | 1 | 2): number;
read(): number;
readAt(position: number): number;
writeAt(position: number, value: number): Machine;
write(value: number): Machine;
run(): Promise<Machine>;
tickOnce(): Promise<Machine>;
tickOutput(): Promise<Machine>;
setInput(payload: number): Machine;
readInput(): number;
setOutput(): Machine;
readOutput(): number;
moveCursor(position: number | null): Machine;
debug: () => Partial<Machine>;
halted: boolean;
setHalted: () => void;
};
const isNil = <T>(val: T | null | undefined): val is null | undefined =>
val !== (val ?? !val);
export function createMachine(
memory: number[] = [],
input: number | Wire = 0,
output: number | Wire = 0,
cursor: number = 0,
relative: number = 0
): Machine {
return {
memory: [...memory],
input,
output,
cursor,
mode: 0,
relative,
setMode(mode) {
this.mode = mode;
},
setRelative(rel) {
this.relative = this.relative + rel;
return this;
},
next() {
const _next = [
this.memory[this.cursor],
this.cursor,
this.memory[this.cursor] + this.relative
][this.mode % 10];
this.mode = Math.floor(this.mode / 10);
this.cursor = this.cursor + 1;
return _next;
},
read() {
let next = this.next();
return this.readAt(next);
},
readAt(position) {
return this.memory[position] ?? 0;
},
write(value) {
let next = this.next();
this.memory[next] = value;
return this;
},
writeAt(position, value) {
this.memory[position] = value;
return this;
},
run() {
return new Promise((resolve) => {
try {
while (1) {
operations(this);
}
} catch (e) {
resolve(this);
}
});
},
tickOnce() {
return new Promise((resolve, reject) => {
try {
operations(this);
resolve(this);
} catch (e) {
reject(this);
}
});
},
tickOutput() {
return new Promise((resolve) => {
try {
while (1) {
operations(this, true);
}
} catch (e) {
resolve(this);
}
});
},
setInput(payload) {
if (typeof this.input === "number") {
this.input = payload;
return this;
}
this.input.value = payload;
return this;
},
readInput() {
if (typeof this.input === "number") {
return this.input;
}
return this.input.value;
},
setOutput() {
let next = this.next();
if (typeof this.output === "number") {
this.output = this.memory[next] ?? 0;
return this;
}
this.output.value = this.memory[next] ?? 0;
return this;
},
readOutput() {
if (typeof this.output === "number") {
return this.output;
}
return this.output.value;
},
moveCursor(position) {
if (isNil(position)) return this;
this.cursor = position;
return this;
},
debug() {
return {
input: typeof this.input === "number" ? this.input : this.input.value,
output:
typeof this.output === "number" ? this.output : this.output.value,
cursor: this.cursor,
relative: this.relative,
mode: this.mode
};
},
halted: false,
setHalted() {
this.halted = true;
}
};
}
function operations(memory: Machine, throwOnOutput: boolean = false) {
const opcode = memory.next();
const code = opcode % 100;
memory.setMode(Math.floor(opcode / 100));
switch (code) {
case 1:
return memory.write(memory.read() + memory.read());
case 2:
return memory.write(memory.read() * memory.read());
case 3:
return memory.write(memory.readInput());
case 4: {
if (throwOnOutput) {
throw memory.setOutput();
}
return memory.setOutput();
}
case 5: {
// jump if true
let param = memory.read();
let target = memory.read();
return memory.moveCursor(param !== 0 ? target : null);
}
case 6: {
// jump if false
let param = memory.read();
let target = memory.read();
return memory.moveCursor(param === 0 ? target : null);
}
case 7:
// less than
return memory.write(memory.read() < memory.read() ? 1 : 0);
case 8:
// equal
return memory.write(memory.read() === memory.read() ? 1 : 0);
case 9:
return memory.setRelative(memory.read());
case 99:
default:
memory.setHalted();
throw "Halt";
}
}
export const pipe = (memories: Machine[]) => async (
initial: number
): Promise<number> => {
let prev = initial;
await Promise.all(memories.map((memory) => memory.tickOnce()));
for await (const memory of memories) {
memory.setInput(prev);
await memory.run();
prev = memory.readOutput();
}
return prev;
};
export const loop = (memories: Machine[]) => async (seed: number) => {
await Promise.all(memories.map((memory) => memory.tickOnce()));
memories[0].setInput(seed);
while (memories.some(({ halted }) => !halted)) {
for await (const memory of memories) {
await memory.tickOutput();
}
}
const [last] = memories.slice(-1);
return last.readOutput();
};