Skip to content

Commit 7114433

Browse files
committed
10/2024
1 parent 3e0ed27 commit 7114433

File tree

2 files changed

+129
-11
lines changed

2 files changed

+129
-11
lines changed

2024/10/index.ts

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { getPuzzle } from "../../utils";
2+
3+
const puzzleInput = getPuzzle(__dirname).trim();
4+
5+
const topoMap = puzzleInput.split("\n").map((row) => row.split("").map(Number));
6+
7+
const MAP_HEIGHT = topoMap.length;
8+
const MAP_WIDTH = topoMap[0].length;
9+
10+
const DIRECTIONS = {
11+
UP: [0, -1],
12+
RIGHT: [1, 0],
13+
DOWN: [0, 1],
14+
LEFT: [-1, 0],
15+
};
16+
17+
const getStartingPoints = () => {
18+
const startingPoints: Array<[number, number]> = [];
19+
20+
for (let y = 0; y < topoMap.length; y++) {
21+
for (let x = 0; x < topoMap[0].length; x++) {
22+
const cell = topoMap[y][x];
23+
24+
if (cell === 0) {
25+
startingPoints.push([x, y]);
26+
}
27+
}
28+
}
29+
30+
return startingPoints;
31+
};
32+
33+
// Part 1
34+
(() => {
35+
console.time("part 1");
36+
const startingPoints = getStartingPoints();
37+
let totalScore = 0;
38+
39+
for (const [startingX, startingY] of startingPoints) {
40+
const reachedGoals = new Set<string>();
41+
const queue = [[startingX, startingY]];
42+
43+
while (queue.length) {
44+
const [x, y] = queue.pop();
45+
const currentCell = topoMap[y][x];
46+
47+
for (const direction in DIRECTIONS) {
48+
const [dirX, dirY] = DIRECTIONS[direction];
49+
50+
const newX = x + dirX;
51+
const newY = y + dirY;
52+
53+
if (newX >= MAP_WIDTH || newX < 0 || newY >= MAP_HEIGHT || newY < 0) {
54+
continue;
55+
}
56+
const newCell = topoMap[newY][newX];
57+
58+
if (newCell === currentCell + 1) {
59+
if (newCell === 9) {
60+
reachedGoals.add(`${newX},${newY}`);
61+
continue;
62+
}
63+
64+
queue.push([newX, newY]);
65+
}
66+
}
67+
}
68+
69+
totalScore += reachedGoals.size;
70+
}
71+
72+
console.log("part 1 totalScore ::", totalScore);
73+
console.timeEnd("part 1");
74+
})();
75+
76+
// Part 2
77+
(() => {
78+
console.time("part 2");
79+
const startingPoints = getStartingPoints();
80+
let totalScore = 0;
81+
82+
for (const [startingX, startingY] of startingPoints) {
83+
let score = 0;
84+
const queue = [[startingX, startingY]];
85+
86+
while (queue.length) {
87+
const [x, y] = queue.pop();
88+
const currentCell = topoMap[y][x];
89+
90+
for (const direction in DIRECTIONS) {
91+
const [dirX, dirY] = DIRECTIONS[direction];
92+
93+
const newX = x + dirX;
94+
const newY = y + dirY;
95+
96+
if (newX >= MAP_WIDTH || newX < 0 || newY >= MAP_HEIGHT || newY < 0) {
97+
continue;
98+
}
99+
const newCell = topoMap[newY][newX];
100+
101+
if (newCell === currentCell + 1) {
102+
if (newCell === 9) {
103+
score += 1;
104+
continue;
105+
}
106+
107+
queue.push([newX, newY]);
108+
}
109+
}
110+
}
111+
112+
totalScore += score;
113+
}
114+
115+
console.log("part 2 totalScore ::", totalScore);
116+
console.timeEnd("part 2");
117+
})();

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
55
## 2024
66

7-
| Day | Part 1 | Part 2 |
8-
| :---------------------------------------: | :----: | :----: |
9-
| [09](https://adventofcode.com/2024/day/9) |||
10-
| [08](https://adventofcode.com/2024/day/8) |||
11-
| [07](https://adventofcode.com/2024/day/7) |||
12-
| [06](https://adventofcode.com/2024/day/6) |||
13-
| [05](https://adventofcode.com/2024/day/5) ||  ✅ |
14-
| [04](https://adventofcode.com/2024/day/4) ||  ✅ |
15-
| [03](https://adventofcode.com/2024/day/3) ||  ✅ |
16-
| [02](https://adventofcode.com/2024/day/2) ||  ✅ |
17-
| [01](https://adventofcode.com/2024/day/1) ||  ✅ |
7+
| Day | Part 1 | Part 2 |
8+
| :----------------------------------------: | :----: | :----: |
9+
| [10](https://adventofcode.com/2024/day/10) |||
10+
| [09](https://adventofcode.com/2024/day/9) |||
11+
| [08](https://adventofcode.com/2024/day/8) |||
12+
| [07](https://adventofcode.com/2024/day/7) |||
13+
| [06](https://adventofcode.com/2024/day/6) |||
14+
| [05](https://adventofcode.com/2024/day/5) ||  ✅ |
15+
| [04](https://adventofcode.com/2024/day/4) ||  ✅ |
16+
| [03](https://adventofcode.com/2024/day/3) ||  ✅ |
17+
| [02](https://adventofcode.com/2024/day/2) ||  ✅ |
18+
| [01](https://adventofcode.com/2024/day/1) ||  ✅ |
1819

1920
## 2023
2021

0 commit comments

Comments
 (0)