/
day17.js
255 lines (239 loc) · 6.25 KB
/
day17.js
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
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]) {
arr[index] = valueA + valueB;
}
function multiply(arr, [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!");
}
arr[index] = value;
}
function output(arr, [valueA]) {
return { output: valueA };
}
function jumpIfTrue(arr, [valueA, valueB]) {
if (valueA !== 0) return { pointer: valueB };
}
function jumpIfFalse(arr, [valueA, valueB]) {
if (valueA === 0) return { pointer: valueB };
}
function lessThan(arr, [valueA, valueB, index]) {
arr[index] = valueA < valueB ? 1 : 0;
}
function equals(arr, [valueA, valueB, index]) {
arr[index] = valueA === valueB ? 1 : 0;
}
function offsetBase(arr, [valueA], { base }) {
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,
initialBase = 0,
intitialPointer = 0
} = {}
) {
let base = initialBase;
let pointer = intitialPointer;
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) {
if (outputFn) outputFn(result.output);
if (yieldOnOutput)
yield { output: result.output, memory: arr, pointer, base };
}
}
return arr;
}
function parseInput(input) {
if (Array.isArray(input)) return input;
return input.split(",").map(str => parseInt(str, 10));
}
// todays code starts here, no additions to intcode made
function getImage(input) {
let output = [];
runIntcode(input, {
outputFn: value => {
output.push(value);
}
}).next();
return output.map(charCode => String.fromCharCode(charCode)).join("");
}
function getAlignmentParameters(input) {
const arr = getImage(input).split("\n");
const width = arr[0].length;
const height = arr.length;
let total = 0;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (arr[y][x] === ".") continue;
const tiles = [
arr[y][x - 1], // left
arr[y][x + 1], // right
arr[y - 1] && arr[y - 1][x], // up
arr[y + 1] && arr[y + 1][x]
].filter(tile => tile && tile !== ".");
if (tiles.length > 2) {
total += x * y;
}
}
}
return total;
}
const move = [
(x, y) => [x, y - 1], // up
(x, y) => [x + 1, y], // right
(x, y) => [x, y + 1], // down
(x, y) => [x - 1, y] // left
];
const arrows = ["^", ">", "v", "<"];
function incrementDir(dir, increment) {
return (((dir - increment) % 4) + 4) % 4;
}
function getTile(arr, [x, y]) {
return arr[y] && arr[y][x];
}
function getPath(image) {
// assumptions being made
// - there is a single continuous path
// - corners are never ambiguous (never adjacent to other tiles, a corner
// is a path segment with path segments in exactly two other directions)
// - there is an end with a tile that only has one adjacent tile
const arr = image.split("\n");
const width = arr[0].length;
const height = arr.length;
const start = image.split("").findIndex(tile => arrows.indexOf(tile) !== -1);
let y = Math.floor(start / (width + 1));
let x = start % (width + 1);
let dir = arrows.indexOf(arr[y][x]);
let i = 0;
const path = [];
while (true) {
let [x2, y2] = move[dir](x, y);
let steps = 0;
while (getTile(arr, [x2, y2]) === "#") {
steps += 1;
x = x2;
y = y2;
[x2, y2] = move[dir](x, y);
}
if (steps) {
path.push(`${steps}`);
}
const rightDir = incrementDir(dir, -1);
const leftDir = incrementDir(dir, 1);
if (getTile(arr, move[rightDir](x, y)) === "#") {
path.push("R");
dir = rightDir;
} else if (getTile(arr, move[leftDir](x, y)) === "#") {
path.push("L");
dir = leftDir;
} else {
return path;
}
}
}
function guideRobot(intcode) {
// I did this manually by taking the path and looking for patterns
const a = "L,12,L,8,R,10,R,10";
const b = "L,6,L,4,L,12";
const c = "R,10,L,8,L,4,R,10";
const routine = "A,B,A,B,C,B,A,C,B,C";
let output = [];
let input = [
...routine.split(""),
"\n",
...a.split(""),
"\n",
...b.split(""),
"\n",
...c.split(""),
"\n",
"n",
"\n"
].map(char => char.charCodeAt(0));
runIntcode(intcode, {
inputFn: () => {
const message = output
.map(charCode => String.fromCharCode(charCode))
.join("");
console.log(message);
output = [];
return input.shift();
},
outputFn: value => {
output.push(value);
}
}).next();
return output.pop();
}
function getPatterns(inputPath) {
// assumptions being made
// - pattern always starts with a turn
// - pattern never ends on a turn
let pathString = inputPath.join(",");
}