-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday09.js
More file actions
127 lines (121 loc) · 3.21 KB
/
day09.js
File metadata and controls
127 lines (121 loc) · 3.21 KB
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
const operations = [
[add, 3, true],
[multiply, 3, true],
[input, 1, true],
[output, 1, false],
[jumpIfTrue, 2, false],
[jumpIfFalse, 2, false],
[lessThan, 3, true],
[equals, 3, true],
[offsetBase, 1, false]
];
function getArgValues(arr, args, op, writes, { base }) {
const modes = `0000${op}`
.slice(0, -2)
.slice(-3)
.split("")
.reverse();
return args.map((arg, i) => {
const mode = parseInt(modes[i], 10);
const isInWriteMode = writes && i === args.length - 1;
switch (mode) {
case 0:
return isInWriteMode ? arg : arr[arg] || 0;
case 1:
return arg;
case 2:
return isInWriteMode ? base + arg : arr[base + arg] || 0;
default:
throw new Error(`Invalid mode ${mode} specified`);
}
});
}
function add(arr, [valueA, valueB, index]) {
console.log("add", valueA, valueB, index);
arr[index] = valueA + valueB;
}
function multiply(arr, [valueA, valueB, index]) {
console.log("multiply", valueA, valueB, index);
arr[index] = valueA * valueB;
}
function input(arr, [index], { inputFn }) {
const value = parseInt(inputFn("input:"), 10);
if (Number.isNaN(value)) {
throw new Error("Invalid input!");
}
console.log("input", index, value);
arr[index] = value;
}
function output(arr, [valueA]) {
console.log("output", valueA);
return { output: valueA };
}
function jumpIfTrue(arr, [valueA, valueB]) {
console.log("jumpIfTrue", valueA, valueB);
if (valueA !== 0) return { pointer: valueB };
}
function jumpIfFalse(arr, [valueA, valueB]) {
console.log("jumpIfFalse", valueA, valueB);
if (valueA === 0) return { pointer: valueB };
}
function lessThan(arr, [valueA, valueB, index]) {
console.log("lessThan", valueA, valueB, index);
arr[index] = valueA < valueB ? 1 : 0;
}
function equals(arr, [valueA, valueB, index]) {
console.log("equals", valueA, valueB, index);
arr[index] = valueA === valueB ? 1 : 0;
}
function offsetBase(arr, [valueA], { base }) {
console.log("offsetBase", valueA);
return { base: base + valueA };
}
function doOperation(arr, opIndex, options) {
const opCode = arr[opIndex] % 100;
if (opCode === 99) return {};
const [operation, arity, writes] = operations[opCode - 1];
const argStartIndex = opIndex + 1;
const argEndIndex = argStartIndex + arity;
const args = getArgValues(
arr,
arr.slice(argStartIndex, argEndIndex),
arr[opIndex],
writes,
options
);
const { pointer = argEndIndex, output, base } =
operation(arr, args, options) || {};
return { pointer, output, base };
}
function* runIntcode(
inputArr,
{
inputFn = window.prompt,
outputFn = console.log,
yieldOnOutput = false
} = {}
) {
let base = 0;
let pointer = 0;
const arr = parseInput(inputArr);
while (pointer !== undefined) {
const result = doOperation(arr, pointer, {
base,
inputFn,
outputFn
});
pointer = result.pointer;
if (result.base !== undefined) {
base = result.base;
}
if (result.output !== undefined) {
outputFn(result.output);
if (yieldOnOutput) yield result.output;
}
}
return arr;
}
function parseInput(input) {
if (Array.isArray(input)) return input;
return input.split(",").map(str => parseInt(str, 10));
}