-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
179 lines (149 loc) · 4.09 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
import run from "aocrunner";
import { TupleSet } from "../utils/tuple-set/index.js";
const MAX_ITERS = 1_000_000_000;
type Direction = "up" | "right" | "down" | "left";
type Coord = [number, number];
type CoordWithDirection = [number, number, Direction];
type Input = {
start: Coord;
direction: Direction;
obstacles: TupleSet<Coord>;
numRows: number;
numCols: number;
};
const parseInput = (rawInput: string): Input => {
let start: Coord;
const obstacles = new TupleSet<Coord>();
let numRows = 0;
let numCols = 0;
rawInput.split("\n").forEach((row, r) =>
row.split("").forEach((char, c) => {
if (char === "^") start = [r, c];
if (char === "#") obstacles.add([r, c]);
numRows = Math.max(numRows, r + 1);
numCols = Math.max(numCols, c + 1);
}),
);
return {
start: start!,
direction: "up",
obstacles,
numRows,
numCols,
};
};
const turn = (coord: CoordWithDirection): CoordWithDirection => {
const direction = coord[2];
switch (direction) {
case "up":
return [coord[0], coord[1], "right"];
case "right":
return [coord[0], coord[1], "down"];
case "down":
return [coord[0], coord[1], "left"];
case "left":
return [coord[0], coord[1], "up"];
}
};
const move = (coord: CoordWithDirection): CoordWithDirection => {
const [r, c, direction] = coord;
switch (direction) {
case "up":
return [r - 1, c, direction];
case "right":
return [r, c + 1, direction];
case "down":
return [r + 1, c, direction];
case "left":
return [r, c - 1, direction];
}
};
const isObstacle = (coord: Coord | CoordWithDirection, input: Input): boolean =>
input.obstacles.has([coord[0], coord[1]]);
const isOutOfBounds = (
coord: Coord | CoordWithDirection,
input: Input,
): boolean => {
const [r, c] = coord;
return r < 0 || r >= input.numRows || c < 0 || c >= input.numCols;
};
const getGuardCoords = (
input: Input,
): { coords: TupleSet<CoordWithDirection>; isLoop: boolean } => {
let guardCoord: CoordWithDirection = [...input.start, "up"];
const guardCoords = new TupleSet<CoordWithDirection>();
guardCoords.add(guardCoord);
for (let i = 0; i < MAX_ITERS; i++) {
let nextCoord = move(guardCoord);
if (isOutOfBounds(nextCoord, input)) break;
if (isObstacle(nextCoord, input)) {
nextCoord = turn(guardCoord);
}
guardCoord = nextCoord;
// If we are somewhere we have already been, stop because we will loop
if (guardCoords.has(guardCoord))
return { coords: guardCoords, isLoop: true };
guardCoords.add(guardCoord);
}
return { coords: guardCoords, isLoop: false };
};
const eq = <T extends unknown[]>(a: T, b: T): boolean =>
a.length === b.length && a.every((_, i) => a[i] === b[i]);
const getObstacleLoopCoords = (input: Input): Coord[] => {
// The new obstacle must be placed somewhere in the guards path otherwise it would have no effect
const possibleCoords = getGuardCoords(input).coords.values({
depth: 2,
}) as unknown as Coord[];
const obstacleLoopCoords = possibleCoords.filter((possibleCoord) => {
if (eq(possibleCoord, input.start)) return false;
input.obstacles.add(possibleCoord);
const { isLoop } = getGuardCoords(input);
input.obstacles.remove(possibleCoord);
return isLoop;
});
return obstacleLoopCoords;
};
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
const guardCoords = getGuardCoords(input).coords.values({ depth: 2 });
return guardCoords.length.toString();
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
const coords = getObstacleLoopCoords(input);
return coords.length.toString();
};
const exampleInput = `
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
`;
run({
part1: {
tests: [
{
input: exampleInput,
expected: "41",
},
],
solution: part1,
},
part2: {
tests: [
{
input: exampleInput,
expected: "6",
},
],
solution: part2,
},
trimTestInputs: true,
// onlyTests: true,
});