-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13-part2.js
58 lines (46 loc) · 1.04 KB
/
day13-part2.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
const readline = require('readline');
const Computer = require('../common/intcode');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.once('line', line => {
const blocks = new Set();
const getSetKey = (x, y) => x + ',' + y;
let ballX;
let paddleX;
const inputFunction = () => Math.sign(ballX - paddleX);
const intCode = line.split(',').map(n => Number(n));
const computer = new Computer(intCode, inputFunction);
const iterator = computer.run();
computer.memorySet(0, 2);
let score = 0;
while (true) {
let next = iterator.next();
if (next.done) {
break;
}
const x = next.value;
const y = iterator.next().value;
const id = iterator.next().value;
if (x === -1 && y === 0) {
score = id;
if (blocks.size === 0) {
break;
}
} else {
const setKey = getSetKey(x, y);
if (id === 2) {
blocks.add(setKey);
} else {
blocks.delete(setKey);
}
if (id === 3) {
paddleX = x;
} else if (id === 4) {
ballX = x;
}
}
}
console.log(score);
});