-
Notifications
You must be signed in to change notification settings - Fork 1
/
day-16.js
191 lines (164 loc) · 6.47 KB
/
day-16.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
const exp = require('constants');
const fs = require('fs');
const fileName = "data/day-16-input.txt";
function solveBoard(board, beams) {
let time = 0;
let beamsActive = true;
let distanceLimit = 1000;
let distanceLimitReached = false;
while(beamsActive && !distanceLimitReached) {
time++;
// Step each beam
// We may add beams as we go so we need to know how many beams we start with
let iterations = beams.length;
for(let i = 0; i < iterations; i++) {
if(!beams[i].isActive) {
continue;
}
if (beams[i].row < 0 || beams[i].row >= board.length || beams[i].col < 0 || beams[i].col >= board[beams[i].row].length) {
beams[i].isActive = false;
continue;
}
beams[i].distance++;
let cell = board[beams[i].row][beams[i].col];
cell.visits++;
if (beams[i].distance > distanceLimit) {
console.log(`Distance limit reached: ${beams[i].distance}`);
distanceLimitReached = true;
break;
}
if (cell.piece == "|") {
if ( beams[i].direction == "E" || beams[i].direction == "W") {
// Stop forking the beam to allow more iterations
beams[i].isActive = false;
cell.splitCount++;
if (cell.splitCount < 10) {
beams.push({row: beams[i].row - 1, col: beams[i].col, direction: "N", isActive: true});
beams.push({row: beams[i].row + 1, col: beams[i].col, direction: "S", isActive: true});
}
}
} else if (cell.piece == "-") {
if ( beams[i].direction == "N" || beams[i].direction == "S") {
// Stop forking the beam to allow more iterations
beams[i].isActive = false;
cell.splitCount++;
if (cell.splitCount < 10) {
beams.push({row: beams[i].row, col: beams[i].col + 1, direction: "E", isActive: true});
beams.push({row: beams[i].row, col: beams[i].col - 1, direction: "W", isActive: true});
}
}
} else if (cell.piece == "/") {
if ( beams[i].direction == "N") {
beams[i].direction = "E";
} else if ( beams[i].direction == "S") {
beams[i].direction = "W";
} else if ( beams[i].direction == "E") {
beams[i].direction = "N";
} else if ( beams[i].direction == "W") {
beams[i].direction = "S";
}
} else if (cell.piece == "\\") {
if ( beams[i].direction == "N") {
beams[i].direction = "W";
} else if ( beams[i].direction == "S") {
beams[i].direction = "E";
} else if ( beams[i].direction == "E") {
beams[i].direction = "S";
} else if ( beams[i].direction == "W") {
beams[i].direction = "N";
}
}
if(beams[i].direction == "E") {
beams[i].col++;
} else if(beams[i].direction == "W") {
beams[i].col--;
} else if(beams[i].direction == "N") {
beams[i].row--;
} else if(beams[i].direction == "S") {
beams[i].row++;
}
}
beams = beams.filter(beam => beam.isActive);
beamsActive = beams.length > 0;
}
}
function calculateTotalEnergized(board) {
let totalEnergized = 0;
for(let i = 0; i < board.length; i++) {
for(let j = 0; j < board[i].length; j++) {
if (board[i][j].visits > 0) {
totalEnergized++;
}
}
}
return totalEnergized;
}
function part1() {
const file = fs.readFileSync(fileName, 'utf8');
const lines = file.split(/\r?\n/);
let board = [];
for(let i = 0; i < lines.length; i++) {
board.push([]);
for(let j = 0; j < lines[i].length; j++) {
let value = {
piece: lines[i][j],
visits: 0,
splitCount: 0
}
board[i].push(value);
}
}
let beams = [{row: 0, col: 0, direction: "E", isActive: true, distance: 0}];
solveBoard(board, beams);
let totalEnergized = calculateTotalEnergized(board);
console.log(`Part 1: ${totalEnergized}`);
}
function part2() {
const file = fs.readFileSync(fileName, 'utf8');
const lines = file.split(/\r?\n/);
let board = [];
for(let i = 0; i < lines.length; i++) {
board.push([]);
for(let j = 0; j < lines[i].length; j++) {
let value = {
piece: lines[i][j],
visits: 0,
splitCount: 0
}
board[i].push(value);
}
}
let energizedTestResults = [];
// West edges
for(let i = 0; i < board.length; i++) {
let newBoard = JSON.parse(JSON.stringify(board));
let beams = [{row: i, col: 0, direction: "E", isActive: true, distance: 0}];
solveBoard(newBoard, beams);
energizedTestResults.push(calculateTotalEnergized(newBoard));
}
// East edges
for(let i = 0; i < board.length; i++) {
let newBoard = JSON.parse(JSON.stringify(board));
let beams = [{row: i, col: board[0].length - 1, direction: "W", isActive: true, distance: 0}];
solveBoard(newBoard, beams);
energizedTestResults.push(calculateTotalEnergized(newBoard));
}
// North edges
for(let i = 0; i < board[0].length; i++) {
let newBoard = JSON.parse(JSON.stringify(board));
let beams = [{row: 0, col: i, direction: "S", isActive: true, distance: 0}];
solveBoard(newBoard, beams);
energizedTestResults.push(calculateTotalEnergized(newBoard));
}
// South edges
for(let i = 0; i < board[0].length; i++) {
let newBoard = JSON.parse(JSON.stringify(board));
let beams = [{row: board.length - 1, col: i, direction: "N", isActive: true, distance: 0}];
solveBoard(newBoard, beams);
energizedTestResults.push(calculateTotalEnergized(newBoard));
}
let maxEnergized = Math.max(...energizedTestResults);
console.log(`Max energized: ${maxEnergized}`);
}
part1();
part2();