Skip to content

Commit 3289d5d

Browse files
committed
06/2024 (part 2)
1 parent 9f9bfcd commit 3289d5d

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

2024/06/index.ts

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ type GuardPosition = {
2828
const startY = map.findIndex((row) => row.includes(GUARD_STARTING_CHAR));
2929
const startX = map[startY].indexOf(GUARD_STARTING_CHAR);
3030

31-
const getVisitedCells = (
32-
mapInput: typeof map,
33-
keyFormatter: (guardPos: GuardPosition) => string = ({ x, y }) => `${x},${y}`,
34-
enableLoopDetection = false
35-
) => {
31+
const getVisitedCells = ({
32+
enableLoopDetection,
33+
keyFormatter = ({ x, y }) => `${x},${y}`,
34+
mapInput,
35+
}: {
36+
enableLoopDetection?: boolean;
37+
keyFormatter?: (guardPos: GuardPosition) => string;
38+
mapInput: typeof map;
39+
}) => {
3640
let guardPosition: GuardPosition = { x: startX, y: startY, direction: "UP" };
3741
let loopDetected = false;
3842

@@ -42,8 +46,8 @@ const getVisitedCells = (
4246
while (true) {
4347
const nextMove = DIRECTIONS[guardPosition.direction];
4448
const nextPosition = {
45-
x: Math.abs(guardPosition.x + nextMove.x),
46-
y: Math.abs(guardPosition.y + nextMove.y),
49+
x: guardPosition.x + nextMove.x,
50+
y: guardPosition.y + nextMove.y,
4751
};
4852

4953
if (
@@ -87,8 +91,41 @@ const getVisitedCells = (
8791
(() => {
8892
console.time("part 1");
8993

90-
const { visitedCells } = getVisitedCells(map);
94+
const { visitedCells } = getVisitedCells({ mapInput: map });
9195

9296
console.log("part 1 visitedCells count ::", visitedCells.size);
9397
console.timeEnd("part 1");
9498
})();
99+
100+
// Part 2
101+
(() => {
102+
console.time("part 2");
103+
104+
const { visitedCells } = getVisitedCells({ mapInput: map });
105+
let loopsFound = 0;
106+
107+
for (let visitedCell of visitedCells) {
108+
const [x, y] = visitedCell.split(",").map((n) => parseInt(n));
109+
110+
if (x === startX && y === startY) {
111+
continue;
112+
}
113+
114+
const mapClone = JSON.parse(JSON.stringify(map)) as typeof map;
115+
mapClone[y][x] = OBSTACLE_CHAR;
116+
117+
const { loopDetected } = getVisitedCells({
118+
enableLoopDetection: true,
119+
keyFormatter: (guardPos) =>
120+
`${guardPos.x},${guardPos.y},${guardPos.direction}`,
121+
mapInput: mapClone,
122+
});
123+
124+
if (loopDetected) {
125+
loopsFound += 1;
126+
}
127+
}
128+
129+
console.log("part 2 loopsFound ::", loopsFound);
130+
console.timeEnd("part 2");
131+
})();

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Day | Part 1 | Part 2 |
88
| :---------------------------------------: | :----: | :----: |
9-
| [06](https://adventofcode.com/2024/day/6) ||  - |
9+
| [06](https://adventofcode.com/2024/day/6) || |
1010
| [05](https://adventofcode.com/2024/day/5) ||  ✅ |
1111
| [04](https://adventofcode.com/2024/day/4) ||  ✅ |
1212
| [03](https://adventofcode.com/2024/day/3) ||  ✅ |

0 commit comments

Comments
 (0)