-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11-part1.js
67 lines (54 loc) · 1.08 KB
/
day11-part1.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
const readline = require('readline');
const Computer = require('../common/intcode');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.once('line', line => {
const intCode = line.split(',').map(n => Number(n));
const computer = new Computer(intCode);
const iterator = computer.run();
const panels = new Map();
const getMapKey = pos => pos.x + ',' + pos.y;
let robotPos = {
x: 0,
y: 0,
};
let robotDir = {
x: 0,
y: -1,
};
while (true) {
const mapKey = getMapKey(robotPos);
let input = panels.get(mapKey);
if (input === undefined) {
input = 0;
}
computer.enqueueInput(input);
let next = iterator.next();
if (next.done) {
break;
}
const color = next.value;
panels.set(mapKey, color);
next = iterator.next();
if (next.done) {
break;
}
const turn = next.value;
if (turn) {
robotDir = {
x: -robotDir.y,
y: robotDir.x,
};
} else {
robotDir = {
x: robotDir.y,
y: -robotDir.x,
};
}
robotPos.x += robotDir.x;
robotPos.y += robotDir.y;
}
console.log(panels.size);
});