-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.ts
158 lines (135 loc) · 4.15 KB
/
6.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
import * as fs from "fs";
const input = fs.readFileSync("6-input.txt", "utf8");
// const input: string = `....#.....
// .........#
// ..........
// ..#.......
// .......#..
// ..........
// .#..^.....
// ........#.
// #.........
// ......#...`;
// const input = `.....
// .....
// ....#
// #....
// .^.#.`
const grid = input.split("\n").map(line => line.split(""));
const part1 = () => {
let [xPos, yPos] = [-1, -1];
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] === "^") {
xPos = j;
yPos = i;
}
}
}
let [xDir, yDir] = [0, -1];
let seen: Set<string> = new Set<string>();
seen.add([xPos, yPos].toString());
while (xPos >= 0 && yPos >= 0 && yPos < grid.length && xPos < grid[yPos].length) {
const nextC = grid[yPos + yDir]?.[xPos + xDir];
if (nextC === "#") {
if (xDir === 0 && yDir == -1) {
xDir = 1;
yDir = 0;
} else if (xDir === 1 && yDir === 0) {
xDir = 0;
yDir = 1;
} else if (xDir === 0 && yDir === 1) {
xDir = -1;
yDir = 0;
} else if (xDir === -1 && yDir === 0) {
xDir = 0;
yDir = -1;
}
} else {
xPos += xDir;
yPos += yDir;
seen.add([xPos, yPos].toString());
}
}
return seen.size - 1;
};
const part2 = () => {
function isLoop(grid: string[][], xPos: number, yPos: number, xDir: number, yDir: number): boolean {
let seen: Record<string, boolean> = {};
while (xPos >= 0 && yPos >= 0 && yPos < grid.length && xPos < grid[yPos].length) {
const key = `${xPos},${yPos},${xDir},${yDir}`;
if (seen[key]) {
return true;
}
seen[key] = true;
const nextC = grid[yPos + yDir]?.[xPos + xDir];
if (nextC === "#") {
if (xDir === 0 && yDir == -1) {
xDir = 1;
yDir = 0;
} else if (xDir === 1 && yDir === 0) {
xDir = 0;
yDir = 1;
} else if (xDir === 0 && yDir === 1) {
xDir = -1;
yDir = 0;
} else if (xDir === -1 && yDir === 0) {
xDir = 0;
yDir = -1;
}
} else {
xPos += xDir;
yPos += yDir;
}
}
return false;
}
let [xPos, yPos] = [-1, -1];
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if (grid[i][j] === "^") {
xPos = j;
yPos = i;
}
}
}
let [xDir, yDir] = [0, -1];
let loops = new Set<string>();
let seen = new Set<string>();
while (xPos >= 0 && yPos >= 0 && yPos < grid.length && xPos < grid[yPos].length) {
const nextC = grid[yPos + yDir]?.[xPos + xDir];
if (nextC === "#") {
if (xDir === 0 && yDir == -1) {
xDir = 1;
yDir = 0;
} else if (xDir === 1 && yDir === 0) {
xDir = 0;
yDir = 1;
} else if (xDir === 0 && yDir === 1) {
xDir = -1;
yDir = 0;
} else if (xDir === -1 && yDir === 0) {
xDir = 0;
yDir = -1;
}
} else {
if (nextC && nextC === ".") {
const before = grid[yPos + yDir][xPos + xDir];
grid[yPos + yDir][xPos + xDir] = "#"
if (isLoop(grid, xPos, yPos, xDir, yDir)) {
loops.add([xPos + xDir, yPos + yDir].toString())
}
// backtrack
grid[yPos + yDir][xPos + xDir] = before;
}
xPos += xDir;
yPos += yDir;
}
}
for (const loop of loops) {
console.log(loop);
}
return loops.size;
};
// console.log(part1());
console.log(part2());