-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.js
115 lines (89 loc) · 2.74 KB
/
day11.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
var fs = require("fs");
var Intcode = require("./intcode");
let input = fs.readFileSync(__dirname +"/input.txt").toString().split(",").map(Number);
let intcode = new Intcode();
class Block {
constructor(x,y,color) {
this.x = x;
this.y = y;
this.color = color;
}
}
let data = [];
let painted = 0;
function paint(x, y, color) {
for (let i = 0; i < data.length; i++) {
if (data[i].x == x && data[i].y == y) {
data[i].color = color;
return;
}
}
painted++;
data.push(new Block(x, y, color));
}
function getColor(x, y) {
for (let i = 0; i < data.length; i++) {
if (data[i].x == x && data[i].y == y) {
return data[i].color;
}
}
return 0;
}
function part1() {
intcode.input = input;
intcode.start();
let posX = 0, posY = 0, direction = 0; //0-up, 1-right, 2-down, 3-left
while (!intcode.halted) {
let currentColor = getColor(posX, posY);
intcode.userInput.push(currentColor);
intcode.continue();
paint(posX, posY, intcode.output[intcode.output.length - 2]);
let rotation = intcode.output[intcode.output.length - 1];
direction += rotation * 2 - 1;
if (direction < 0) direction += 4;
if (direction >= 4) direction -= 4;
switch (direction) {
case 0: posY--; break;
case 1: posX++; break;
case 2: posY++; break;
case 3: posX--; break;
}
}
console.log(painted);
}
function part2() {
intcode.input = input;
intcode.start();
let posX = 0, posY = 0, direction = 0; //0-up, 1-right, 2-down, 3-left
data.push(new Block(0, 0, 1));
let minX = 0, maxX = 0, minY = 0, maxY = 0;
while (!intcode.halted) {
let currentColor = getColor(posX, posY);
intcode.userInput.push(currentColor);
intcode.continue();
paint(posX, posY, intcode.output[intcode.output.length - 2]);
let rotation = intcode.output[intcode.output.length - 1];
if (posX > maxX) maxX = posX;
if (posX < minX) minX = posX;
if (posY > maxY) maxY = posY;
if (posY < minY) minY = posY;
direction += rotation * 2 - 1;
if (direction < 0) direction += 4;
if (direction >= 4) direction -= 4;
switch (direction) {
case 0: posY--; break;
case 1: posX++; break;
case 2: posY++; break;
case 3: posX--; break;
}
}
for (let y = minY; y <= maxY; y++) {
let line = "";
for (let x = minX; x <= maxX; x++) {
let m = getColor(x, y);
line += (m == 1) ? "X" : " ";
}
console.log(line+" ");
}
}
part2();