-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
213 lines (192 loc) · 6.29 KB
/
index.ts
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import run from "aocrunner";
import "../utils/index.js";
const parseInput = (rawInput: string) => {
return rawInput.split("\n").map(line => {
return line.split("");
});
}
type Beam = { x: number, y: number, facing: "N"|"W"|"E"|"S" };
const printGrid = (grid: string[][], beamSpots: Map<string, Beam[]>) => {
for (let y = 0; y < grid.length; y++) {
let line = "";
for (let x = 0; x < grid[y].length; x++) {
const beamsInSpot = beamSpots.get(`${x}-${y}`);
if (grid[y][x] !== ".") {
line += grid[y][x];
} else if (beamsInSpot && beamsInSpot.length > 1) {
line += beamsInSpot.length;
} else if (beamsInSpot) {
const beam = beamsInSpot[0];
if (beam.facing == "N") {
line += "^";
} else if (beam.facing === "E") {
line += ">";
} else if (beam.facing === "S") {
line += "v";
} else if (beam.facing === "W") {
line += "<";
}
} else {
line += grid[y][x];
}
}
console.log(line);
}
};
function getEnergizedSpaces(grid: string[][], initialSpot: Beam) {
const xMax = grid[0].length - 1;
const yMax = grid.length - 1;
const energizedSpaces = new Set<string>();
const beamSpots = new Map<string, Beam[]>();
const beams: Beam[] = [initialSpot];
let ticks = 0;
while (beams.length > 0 && ticks < 1000) {
ticks++;
for (const beam of beams) {
// console.log(`Working on beam ${JSON.stringify(beam)}`);
// The beam wouldn't be here if it wasn't valid, so mark the spot as energized
const beamKey = `${beam.x}-${beam.y}`;
energizedSpaces.add(beamKey);
// Check for it in the list - if we have a match on x, y, and facing, we've been here before
if (!beamSpots.has(beamKey)) {
beamSpots.set(beamKey, [JSON.parse(JSON.stringify(beam))])
} else {
const beamsAtSpot = beamSpots.get(beamKey)!!;
if (beamsAtSpot.findIndex(b => b.x === beam.x && b.y === beam.y && b.facing === beam.facing) > 0) {
// We've been exactly here before. Remove this beam from the list.
const beamIndex = beams.findIndex(b => b.x === beam.x && b.y === beam.y && b.facing === beam.facing);
beams.splice(beamIndex, 1);
continue;
} else {
beamSpots.get(beamKey)!!.push(JSON.parse(JSON.stringify(beam)));
}
}
// If the beam is on a splitter, split, then move on
if ((beam.facing === "E" || beam.facing === "W") && grid[beam.y][beam.x] === "|") {
// console.log("hit the | splitter")
if (beam.y - 1 >= 0) {
beams.push({facing: "N", x: beam.x, y: beam.y - 1});
}
if (beam.y + 1 <= yMax) {
beams.push({facing: "S", x: beam.x, y: beam.y + 1});
}
// Remove the original beam
const beamIndex = beams.findIndex(b => b.x === beam.x && b.y === beam.y && b.facing === beam.facing);
beams.splice(beamIndex, 1);
} else if ((beam.facing === "N" || beam.facing === "S") && grid[beam.y][beam.x] === "-") {
// console.log("hit the - splitter")
if (beam.x - 1 >= 0) {
beams.push({facing: "W", x: beam.x - 1, y: beam.y});
// console.log(`Grid at new loc is: ${grid[beam.y][beam.x - 1]}`)
}
if (beam.x + 1 <= xMax) {
beams.push({facing: "E", x: beam.x + 1, y: beam.y});
// console.log(`Grid at new loc is: ${grid[beam.y][beam.x + 1]}`)
}
// Remove the original beam
const beamIndex = beams.findIndex(b => b.x === beam.x && b.y === beam.y && b.facing === beam.facing);
beams.splice(beamIndex, 1);
} else {
// Adjust facing if needed
if (grid[beam.y][beam.x] === "/") {
if (beam.facing === "E") {
beam.facing = "N";
} else if (beam.facing === "N") {
beam.facing = "E";
} else if (beam.facing === "S") {
beam.facing = "W";
} else if (beam.facing === "W") {
beam.facing = "S";
}
} else if (grid[beam.y][beam.x] === "\\") {
if (beam.facing === "E") {
beam.facing = "S";
} else if (beam.facing === "S") {
beam.facing = "E";
} else if (beam.facing === "N") {
beam.facing = "W";
} else if (beam.facing === "W") {
beam.facing = "N";
}
}
// Then move the lightcycle
if (beam.facing == "N") {
beam.y -= 1;
} else if (beam.facing === "E") {
beam.x += 1;
} else if (beam.facing === "S") {
beam.y += 1;
} else if (beam.facing === "W") {
beam.x -= 1;
}
// console.log(`New beam pos - y: ${beam.y} x: ${beam.x}`)
// The lightcycle has left the grid, remove it
if (beam.x < 0 || beam.x > xMax || beam.y < 0 || beam.y > yMax) {
const beamIndex = beams.findIndex(b => b.x === beam.x && b.y === beam.y && b.facing === beam.facing);
beams.splice(beamIndex, 1);
}
}
}
}
// printGrid(grid, beamSpots);
return energizedSpaces.size;
}
const part1 = (rawInput: string) => {
const grid = parseInput(rawInput);
return getEnergizedSpaces(grid, {x: 0, y: 0, facing: "E"});
};
const part2 = (rawInput: string) => {
const grid = parseInput(rawInput);
const energizedCounts: number[] = [];
for (let y = 0; y < grid.length ; y++) {
energizedCounts.push(getEnergizedSpaces(grid, { x: 0, y, facing: "E" }));
energizedCounts.push(getEnergizedSpaces(grid, { x: grid[0].length - 1, y, facing: "W" }));
}
for (let x = 0; x < grid[0].length - 1; x++) {
energizedCounts.push(getEnergizedSpaces(grid, { x, y: 0, facing: "S" }));
energizedCounts.push(getEnergizedSpaces(grid, { x, y: grid.length - 1, facing: "N" }));
}
return Math.max(...energizedCounts);
};
run({
part1: {
tests: [
{
input: `
.|...\\....
|.-.\\.....
.....|-...
........|.
..........
.........\\
..../.\\\\..
.-.-/..|..
.|....-|.\\
..//.|....`,
expected: 46,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `
.|...\\....
|.-.\\.....
.....|-...
........|.
..........
.........\\
..../.\\\\..
.-.-/..|..
.|....-|.\\
..//.|....`,
expected: 51,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});