-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.js
391 lines (351 loc) · 9.7 KB
/
13.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import R from 'ramda';
import { promises as fs, stat } from 'fs';
import readline from 'readline';
const parseFile = R.compose(
R.map(Number),
R.split(','),
String
);
const readArgs = (state, n, modes) => {
const args = [];
for (const i of [...Array(n)].keys()) {
if (i < modes.length && modes[i] === '1') {
args.push(get(state, state.pointer + 1 + i));
} else if (i < modes.length && modes[i] === '2') {
args.push(get(state, state.rbase + get(state, state.pointer + 1 + i)));
} else {
args.push(get(state, get(state, state.pointer + 1 + i)));
}
}
return args;
}
const get = (state, idx) => {
if (idx >= state.mem.length) {
if (state.backingMem[idx]) return state.backingMem[idx];
state.backingMem[idx] = 0;
return 0;
}
return state.mem[idx];
}
const set = (state, idx, val) => {
if (idx >= state.mem.length) {
state.backingMem[idx] = val;
return state;
}
const newMem = R.update(idx, val, state.mem);
return {
...state,
mem: newMem,
}
}
const parseModes = (op) => {
const m = op.substring(0, op.length - 2).split('').reverse();
return m
}
const effectiveOp = (op) => {
return op.substring(op.length - 2);
}
const askQuestion = (query) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise(resolve => rl.question(query, ans => {
rl.close();
resolve(ans);
}))
}
const keypress = async () => {
process.stdin.setRawMode(true)
return new Promise(resolve => process.stdin.once('data', (key) => {
process.stdin.setRawMode(false)
if (key == '\u001B\u005B\u0043') {
// process.stdout.write('right');
resolve(1)
}
else if (key == '\u001B\u005B\u0044') {
// process.stdout.write('left');
resolve(-1)
} else if (key == '\u0003') {
process.exit();
} else {
resolve(0)
}
// resolve()
}))
}
let c = 0;
const step = async (state, input, output) => {
let op = state.mem[state.pointer];
op = String(op).padStart(2, '0');
// Input
if (effectiveOp(op) === '03') {
let targetIdx = state.mem[state.pointer + 1];
if (parseModes(op)[0] == '2') {
targetIdx += state.rbase;
}
const value = await input();
return {
...state,
pointer: state.pointer + 2,
mem: set(state, targetIdx, value).mem,
};
} else if (effectiveOp(op) === '01') { // Sum
const args = readArgs(state, 2, parseModes(op));
let targetIdx = state.mem[state.pointer + 3];
if(parseModes(op)[2] == '2') {
targetIdx += state.rbase;
}
return {
...state,
pointer: state.pointer + 4,
mem: set(state, targetIdx, R.sum(args)).mem,
};
} else if (effectiveOp(op) === '02') { // Product
const args = readArgs(state, 2, parseModes(op));
let targetIdx = state.mem[state.pointer + 3];
if(parseModes(op)[2] == '2') {
targetIdx += state.rbase;
}
return {
...state,
pointer: state.pointer + 4,
mem: set(state, targetIdx, R.product(args)).mem,
};
} else if (effectiveOp(op) === '99') { // Exit
console.log("Finished");
return {
...state,
pointer: -1
};
} else if (effectiveOp(op) === '04') { // Output
const args = readArgs(state, 1, parseModes(op));
output(args[0]);
return {
...state,
pointer: state.pointer + 2,
};
} else if (effectiveOp(op) === '05') { // Jump If True
const args = readArgs(state, 2, parseModes(op));
if (args[0] !== 0) {
return {
...state,
pointer: args[1]
};
} else {
return {
...state,
pointer: state.pointer + 3,
};
}
} else if (effectiveOp(op) === '06') { // Jump if False
const args = readArgs(state, 2, parseModes(op));
if (args[0] === 0) {
return {
...state,
pointer: args[1],
};
} else {
return {
...state,
pointer: state.pointer + 3,
};
}
} else if (effectiveOp(op) === '07') { // less than
const args = readArgs(state, 2, parseModes(op));
let targetIdx = state.mem[state.pointer + 3];
if(parseModes(op)[2] == '2') {
targetIdx += state.rbase;
}
if (args[0] < args[1]) {
return {
...state,
pointer: state.pointer + 4,
mem: set(state, targetIdx, 1).mem,
};
} else {
return {
...state,
pointer: state.pointer + 4,
mem: set(state, targetIdx, 0).mem,
};
}
} else if (effectiveOp(op) === '08') { // Equals
const args = readArgs(state, 2, parseModes(op));
let targetIdx = state.mem[state.pointer + 3];
// console.log("args: ", args);
if(parseModes(op)[2] == '2') {
targetIdx += state.rbase;
}
if (args[0] === args[1]) {
return {
...state,
pointer: state.pointer + 4,
mem: set(state, targetIdx, 1).mem,
};
} else {
return {
...state,
pointer: state.pointer + 4,
mem: set(state, targetIdx, 0).mem,
};
}
} else if (effectiveOp(op) === '09') { // Adjust Rbase
return op09(state, op);
}
}
const op09 = (state, op) => {
const args = readArgs(state, 1, parseModes(op));
return {
...state,
pointer: state.pointer + 2,
rbase: state.rbase + args[0],
}
}
const runProgram = async (program, input, output) => {
let state = {
pointer: 0,
mem: program,
rbase: 0,
backingMem: {},
}
while (state.pointer !== -1) {
state = await step(state, input, output);
// console.log("after: ", state);
}
}
const tile = (id) => {
switch (id) {
case 0:
return ' ';
case 1:
return 'x';
case 2:
return 'B';
case 3:
return '+';
case 4:
return 'o'
}
}
const run = async () => {
const input = await fs.readFile('/Users/bhaines/Downloads/input.txt');
const parsed = parseFile(input);
const screen = [...Array(25)].map(x => [...Array(36)].fill('x'))
let outs = []; // x y id
print(screen);
parsed[0] = 2;
let prevBallPos = null;
runProgram(parsed,
async () => {
// const inp = await askQuestion("joystick input: ");
const inp = await keypress();
return inp
// return inp == 'j' ? -1 : (inp == '' ? 0 : 1);
// const paddleDest = findPath(screen, ballPos, prevBallPos);
// const paddlePos = findItem(screen, '_');
// if (paddlePos > paddleDest) {
// return -1;
// } else if (paddlePos == paddleDest) {
// return 0;
// } else {
// return 1;
// }
},
(out) => {
outs.push(out);
if (outs.length == 3) {
let shouldPrint = screen[outs[1]][outs[0]] != outs[3]
if (outs[0] == -1 && outs[1] == 0) {
shouldPrint = true;
screen[0][0] = outs[2];
} else {
screen[outs[1]][outs[0]] = tile(outs[2]);
if (tile(outs[2]) == 'o') {
const curPos = findItem(screen, 'o');
if (prevBallPos && curPos) {
replaceAll(screen, '-', ' ')
replaceAll(screen, '*', 'B')
findPath(screen, curPos, prevBallPos)
}
prevBallPos = [...curPos];
}
}
clear();
if (shouldPrint) {
// console.log("Ball: ", findItem(screen, 'o'), " Paddle: ", findItem(screen, '+'));
print(screen);
}
outs = [];
}
});
}
const clear = () => {
process.stdout.write('\u001B[2J\u001B[0;0f')
}
// paddle lives at y=22
const findItem = (screen, target) => {
for (let y = 0; y < screen.length; y++) {
for (let x = 0; x < screen[0].length; x++) {
if (screen[y][x] == target) return [x, y];
}
}
}
const replaceAll = (screen, target, w) => {
for (let y = 0; y < screen.length; y++) {
for (let x = 0; x < screen[0].length; x++) {
if (screen[y][x] == target) screen[y][x] = w;
}
}
}
const nextPos = (screen, x, y, goingRight, goingUp) => {
// const copy = screen.map(x => [...x]);
const nextY = y + (goingUp ? -1 : 1)
const nextX = x + (goingRight ? 1 : -1)
const nextYCell = screen[nextY][x];
const nextXCell = screen[y][nextX];
const nextDiagCell = screen[nextY][nextX];
if (['B','x'].includes(nextYCell) && !['B','x'].includes(nextXCell)) {
if (screen[nextY][x] == 'B') screen[nextY][x] = '*';
return [x, y, goingRight, !goingUp];
}
if (['B','x'].includes(nextXCell) && !['B','x'].includes(nextYCell)) {
if (screen[y][nextX] == 'B') screen[y][nextX] = '*';
return [x, y, !goingRight, goingUp];
}
if (['B','x'].includes(nextXCell) && ['B','x'].includes(nextYCell)) {
if (screen[y][nextX] == 'B') screen[y][nextX] = '*';
if (screen[nextY][x] == 'B') screen[nextY][x] = '*';
return [x, y, !goingRight, !goingUp];
}
if (['B','x'].includes(nextDiagCell)) {
if (screen[nextY][nextX] == 'B') screen[nextY][nextX] = '*';
return [x, y, !goingRight, !goingUp];
}
return [nextX, nextY, goingRight, goingUp];
}
const findPath = (screen, ballPos, prevPos) => {
// const screenCopy = screen.map(x => [...x]);
let goingUp = ballPos[1] - prevPos[1] > 0 ? false : true;
let goingRight = ballPos[0] - prevPos[0] > 0 ? true : false;
let curPos = [...ballPos];
// console.log("finding path from :", curPos);
while (curPos[1] != 22) {
// console.log(curPos);
if (screen[curPos[1]][curPos[0]] == ' ') screen[curPos[1]][curPos[0]] = '-';
const [curX, curY] = curPos;
const nextState = nextPos(screen, curX, curY, goingRight, goingUp)
const newX = nextState[0];
const newY = nextState[1];
goingRight = nextState[2];
goingUp = nextState[3];
curPos = [newX, newY]
}
return curPos[0];
}
const print = (screen) => {
for (const row of screen) {
console.log(row.join(''))
}
}
run();