-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday16.js
138 lines (123 loc) · 3.29 KB
/
day16.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
const {fetchData, sum} = require('./util.js');
const day = 16;
let file = 'sample';
console.info(`D${day}P1 ${file}:`, part1(`./day${day}${file}.txt`));
file = 'input';
console.info(`D${day}P1 ${file}:`, part1(`./day${day}${file}.txt`));
file = 'sample';
console.info(`D${day}P2 ${file}:`, part2(`./day${day}${file}.txt`));
file = 'input';
console.info(`D${day}P2 ${file}:`, part2(`./day${day}${file}.txt`));
function part1(file) {
const grid = readGrid(file);
traceBeam(grid, 0, 0, '>');
return energizedCells(grid).length;
}
function part2(file) {
const grid = readGrid(file);
let result = 0;
for (let c = 0; c < grid[0].length; ++c) {
traceBeam(grid, 0, c, 'v');
result = Math.max(result, energizedCells(grid).length);
resetGrid(grid);
traceBeam(grid, grid.length - 1, c, '^');
result = Math.max(result, energizedCells(grid).length);
resetGrid(grid);
}
for (let r = 0; r < grid.length; ++r) {
traceBeam(grid, r, 0, '>');
result = Math.max(result, energizedCells(grid).length);
resetGrid(grid);
traceBeam(grid, r, grid[0].length - 1, '<');
result = Math.max(result, energizedCells(grid).length);
resetGrid(grid);
}
return result;
}
function readGrid(file) {
return fetchData(file)
.map(line => line.split('').map(c => ({
symbol: c,
enteredFrom: []
})));
}
function traceBeam(grid, row, col, dir) {
if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length) {
return;
}
// console.info('**', {row, col});
const cell = grid[row][col];
if (cell.enteredFrom.includes(dir)) {
return;
}
cell.enteredFrom.push(dir);
for (const exit of exits(cell.symbol, dir)) {
// console.info('** Exit', {exit, cell, dir, row, col});
const [dR, dC, newDir] = exit;
traceBeam(grid, row + dR, col + dC, newDir);
}
}
function energizedCells(grid) {
const result = [];
for (const r of grid) {
for (const c of r) {
if (c.enteredFrom.length) {
result.push([r, c]);
}
}
}
return result;
}
function resetGrid(grid) {
for (const r of grid) {
for (const c of r) {
c.enteredFrom = [];
}
}
}
function exits(symbol, dir) {
if (
symbol === '.' || (
symbol === '-' && '<>'.includes(dir)
) || (
symbol === '|' && '^v'.includes(dir)
))
{
return [follow(dir)];
}
if (symbol === '|') {
return[follow('^'), follow('v')];
}
if (symbol === '-') {
return [follow('<'), follow('>')];
}
if (symbol === '\\') {
const newDir = {
'>' : 'v',
'<' : '^',
'v' : '>',
'^' : '<'
}[dir];
return [follow(newDir)];
}
if (symbol === '/') {
const newDir = {
'>' : '^',
'<' : 'v',
'v' : '<',
'^' : '>'
}[dir];
// console.info('** Bounce // ', dir, newDir);
return [follow(newDir)];
}
console.warn('What?', symbol, dir);
return [];
}
function follow(dir) {
return {
'>': [0, 1],
'<': [0, -1],
'^': [-1, 0],
'v': [1, 0]
}[dir].concat(dir);
}