-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.ts
244 lines (226 loc) · 7.42 KB
/
day6.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const path = './day6input.txt';
const file = Bun.file(path);
const text = await file.text();
let input: string[][] = text.split('\n').map(el => el.split(''));
let initialGuardPosition: { row: number; col: number } | undefined = undefined;
let initialGuardDirection: ('LEFT' | 'TOP' | 'RIGHT' | 'BOTTOM') | undefined =
undefined;
let objectPositions: { row: number; col: number }[] = [];
for (let lineIndex = 0; lineIndex < input.length; lineIndex += 1) {
const line = input[lineIndex];
for (let colIndex = 0; colIndex < line.length; colIndex += 1) {
const col = line[colIndex];
if (col === '^' || col === '>' || col === 'v' || col === '<') {
initialGuardPosition = { row: lineIndex, col: colIndex };
initialGuardDirection =
col === '^'
? 'TOP'
: col === '>'
? 'RIGHT'
: col === 'v'
? 'BOTTOM'
: 'LEFT';
} else if (col === '#') {
objectPositions.push({
col: colIndex,
row: lineIndex,
});
}
}
}
// let outsideOfMace = false;
// while (!outsideOfMace) {
// if (guardDirection === 'TOP') {
// const nextPosition = objectPositions
// .filter(el => el.col === guardPosition?.col && el.row < guardPosition.row)
// .sort((a, b) => {
// return b.row - a.row;
// })[0];
// if (nextPosition) {
// for (let i = guardPosition?.row; i > nextPosition.row; i -= 1) {
// input[i][guardPosition?.col] = 'X';
// }
// guardPosition = { ...nextPosition, row: nextPosition.row + 1 };
// guardDirection = 'RIGHT';
// } else {
// for (let i = guardPosition?.row; i > -1; i -= 1) {
// input[i][guardPosition?.col] = 'X';
// }
// outsideOfMace = true;
// }
// } else if (guardDirection === 'RIGHT') {
// const nextPosition = objectPositions
// .filter(el => el.row === guardPosition?.row && el.col > guardPosition.col)
// .sort((a, b) => {
// return a.col - b.col;
// })[0];
// if (nextPosition) {
// for (let i = guardPosition?.col; i < nextPosition.col; i += 1) {
// input[guardPosition.row][i] = 'X';
// }
// guardPosition = { ...nextPosition, col: nextPosition.col - 1 };
// guardDirection = 'BOTTOM';
// } else {
// for (let i = guardPosition?.col; i < input[0].length; i += 1) {
// input[guardPosition.row][i] = 'X';
// }
// outsideOfMace = true;
// }
// } else if (guardDirection === 'BOTTOM') {
// const nextPosition = objectPositions
// .filter(el => el.col === guardPosition?.col && el.row > guardPosition.row)
// .sort((a, b) => {
// return a.row - b.row;
// })[0];
// if (nextPosition) {
// for (let i = guardPosition?.row; i < nextPosition.row; i += 1) {
// input[i][guardPosition?.col] = 'X';
// }
// guardPosition = { ...nextPosition, row: nextPosition.row - 1 };
// guardDirection = 'LEFT';
// } else {
// for (let i = guardPosition?.row; i < input.length; i += 1) {
// input[i][guardPosition?.col] = 'X';
// }
// outsideOfMace = true;
// }
// } else if (guardDirection === 'LEFT') {
// const nextPosition = objectPositions
// .filter(el => el.row === guardPosition?.row && el.col < guardPosition.col)
// .sort((a, b) => {
// return b.col - a.col;
// })[0];
// if (nextPosition) {
// for (let i = guardPosition?.col; i > nextPosition.col; i -= 1) {
// input[guardPosition?.row][i] = 'X';
// }
// guardPosition = { ...nextPosition, col: nextPosition.col + 1 };
// guardDirection = 'TOP';
// } else {
// for (let i = guardPosition?.col; i > -1; i -= 1) {
// input[guardPosition?.row][i] = 'X';
// }
// outsideOfMace = true;
// }
// }
// }
// console.log(input);
// console.log(
// 'part1 solution',
// input.flat().reduce((acc, cur) => {
// if (cur === 'X') {
// acc += 1;
// }
// return acc;
// }, 0)
// );
const checkLoop = (x: number, y: number) => {
let outsideOfMace = false;
let steps = 0;
const objectPositionsExtended = [...objectPositions, { row: x, col: y }];
let guardDirection = initialGuardDirection;
let guardPosition = initialGuardPosition;
while (true) {
steps += 1;
if (guardDirection === 'TOP') {
const nextPosition = objectPositionsExtended
.filter(
el => el.col === guardPosition?.col && el.row < guardPosition.row
)
.sort((a, b) => {
return b.row - a.row;
})[0];
if (nextPosition) {
for (let i = guardPosition?.row; i > nextPosition.row; i -= 1) {
input[i][guardPosition?.col] = 'X';
}
guardPosition = { ...nextPosition, row: nextPosition.row + 1 };
guardDirection = 'RIGHT';
} else {
for (let i = guardPosition?.row; i > -1; i -= 1) {
input[i][guardPosition?.col] = 'X';
}
outsideOfMace = true;
break;
}
} else if (guardDirection === 'RIGHT') {
const nextPosition = objectPositionsExtended
.filter(
el => el.row === guardPosition?.row && el.col > guardPosition.col
)
.sort((a, b) => {
return a.col - b.col;
})[0];
if (nextPosition) {
for (let i = guardPosition?.col; i < nextPosition.col; i += 1) {
input[guardPosition.row][i] = 'X';
}
guardPosition = { ...nextPosition, col: nextPosition.col - 1 };
guardDirection = 'BOTTOM';
} else {
for (let i = guardPosition?.col; i < input[0].length; i += 1) {
input[guardPosition.row][i] = 'X';
}
outsideOfMace = true;
break;
}
} else if (guardDirection === 'BOTTOM') {
const nextPosition = objectPositionsExtended
.filter(
el => el.col === guardPosition?.col && el.row > guardPosition.row
)
.sort((a, b) => {
return a.row - b.row;
})[0];
if (nextPosition) {
for (let i = guardPosition?.row; i < nextPosition.row; i += 1) {
input[i][guardPosition?.col] = 'X';
}
guardPosition = { ...nextPosition, row: nextPosition.row - 1 };
guardDirection = 'LEFT';
} else {
for (let i = guardPosition?.row; i < input.length; i += 1) {
input[i][guardPosition?.col] = 'X';
}
outsideOfMace = true;
break;
}
} else if (guardDirection === 'LEFT') {
const nextPosition = objectPositionsExtended
.filter(
el => el.row === guardPosition?.row && el.col < guardPosition.col
)
.sort((a, b) => {
return b.col - a.col;
})[0];
if (nextPosition) {
for (let i = guardPosition?.col; i > nextPosition.col; i -= 1) {
input[guardPosition?.row][i] = 'X';
}
guardPosition = { ...nextPosition, col: nextPosition.col + 1 };
guardDirection = 'TOP';
} else {
for (let i = guardPosition?.col; i > -1; i -= 1) {
input[guardPosition?.row][i] = 'X';
}
outsideOfMace = true;
break;
}
}
if (steps > 6e3) {
break;
}
}
return outsideOfMace;
};
let answerPart2 = 0;
for (let x = 0; x < input[0].length; x++) {
for (let y = 0; y < input.length; y++) {
const guardEscaped: boolean = checkLoop(x, y);
if (!guardEscaped) {
answerPart2++;
}
}
}
console.log('answerPart2', answerPart2);
export {};