Skip to content

Commit 6b6cc8d

Browse files
2024 Day 18 complete
1 parent 8e5c4fb commit 6b6cc8d

File tree

5 files changed

+229
-12
lines changed

5 files changed

+229
-12
lines changed

aoc2024/.aocrunner.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,16 @@
259259
},
260260
{
261261
"part1": {
262-
"solved": false,
263-
"result": null,
262+
"solved": true,
263+
"result": "338",
264264
"attempts": [],
265-
"time": null
265+
"time": 13.6978
266266
},
267267
"part2": {
268-
"solved": false,
269-
"result": null,
268+
"solved": true,
269+
"result": "20,44",
270270
"attempts": [],
271-
"time": null
271+
"time": 2.8955
272272
}
273273
},
274274
{

aoc2024/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
[![Day](https://badgen.net/badge/15/%E2%98%85%E2%98%85/green)](src/day15)
2929
[![Day](https://badgen.net/badge/16/%E2%98%85%E2%98%85/green)](src/day16)
3030
[![Day](https://badgen.net/badge/17/%E2%98%85%E2%98%85/green)](src/day17)
31-
![Day](https://badgen.net/badge/18/%E2%98%86%E2%98%86/gray)
31+
[![Day](https://badgen.net/badge/18/%E2%98%85%E2%98%85/green)](src/day18)
3232
![Day](https://badgen.net/badge/19/%E2%98%86%E2%98%86/gray)
3333
![Day](https://badgen.net/badge/20/%E2%98%86%E2%98%86/gray)
3434
![Day](https://badgen.net/badge/21/%E2%98%86%E2%98%86/gray)
@@ -188,9 +188,9 @@ Both parts: 1.586ms
188188

189189
```
190190
Day 18
191-
Time part 1: -
192-
Time part 2: -
193-
Both parts: -
191+
Time part 1: 13.698ms
192+
Time part 2: 2.896ms
193+
Both parts: 16.593ms
194194
```
195195

196196
```
@@ -243,8 +243,8 @@ Both parts: -
243243
```
244244

245245
```
246-
Total stars: 33/50
247-
Total time: 2418.207ms
246+
Total stars: 35/50
247+
Total time: 2434.8ms
248248
```
249249

250250
<!--/RESULTS-->

aoc2024/src/day17/notes.txt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
A mod 8 XOR 3 -> B
2+
A / 2^(A mod 8 XOR 3) -> C
3+
A / 8 -> A
4+
B XOR C XOR 5 -> out -- B and C need to have opposite last 3 bits
5+
JUMP to start if A isnt 0
6+
7+
0: xxxx100000
8+
2: xxxxxx1010
9+
4: 000xxxx100
10+
5: x001xxx101
11+
6: xx010xx110
12+
7: xxx011x111
13+
14+
110xxxx100 | 000
15+
xxxxxxx001 | 010
16+
xxxxxxx011 | 010
17+
xxxxxxx101 | 010
18+
xxxxxxx111 | 010
19+
20+
111
21+
22+
23+
if c ends with 101
24+
b xor 011 = 010
25+
b = 001
26+
27+
000 XOR 011 = 011 -- 0 XOR 3 = 3
28+
001 XOR 011 = 010 -- 1 XOR 3 = 2
29+
010 XOR 011 = 001 -- 2 XOR 3 = 1
30+
011 XOR 011 = 000 -- 3 XOR 3 = 0
31+
100 XOR 011 = 111 -- 4 XOR 3 = 7
32+
101 XOR 011 = 110 -- 5 XOR 3 = 6
33+
110 XOR 011 = 101 -- 6 XOR 3 = 5
34+
111 XOR 011 = 100 -- 7 XOR 3 = 4
35+
36+
000 XOR 101 = 101 -- 0 XOR 5 = 5
37+
001 XOR 101 = 100 -- 1 XOR 5 = 4
38+
010 XOR 101 = 111 -- 2 XOR 5 = 7
39+
011 XOR 101 = 110 -- 3 XOR 5 = 6
40+
100 XOR 101 = 001 -- 4 XOR 5 = 1
41+
101 XOR 101 = 000 -- 5 XOR 5 = 0
42+
110 XOR 101 = 011 -- 6 XOR 5 = 3
43+
111 XOR 101 = 010 -- 7 XOR 5 = 2
44+

aoc2024/src/day18/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 🎄 Advent of Code 2024 - day 18 🎄
2+
3+
## Info
4+
5+
Task description: [link](https://adventofcode.com/2024/day/18)
6+
7+
## Notes
8+
9+
...

aoc2024/src/day18/index.ts

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import run from "aocrunner";
2+
3+
import { MinHeap } from "../utils/index.js";
4+
5+
interface Direction {
6+
dx: number;
7+
dy: number;
8+
}
9+
10+
const allDirections: Direction[] = [
11+
{ dx: -1, dy: 0 },
12+
{ dx: 1, dy: 0 },
13+
{ dx: 0, dy: -1 },
14+
{ dx: 0, dy: 1 },
15+
];
16+
17+
interface Node {
18+
x: number;
19+
y: number;
20+
steps: number;
21+
}
22+
23+
const parseInput = (rawInput: string) =>
24+
rawInput.split("\n").map((line) => line.split(",").map((n) => parseInt(n)));
25+
26+
const makeKey = (node: Node) => `${node.x}|${node.y}`;
27+
28+
const canPath = (grid: number[][], endX: number, endY: number) => {
29+
let visited = new Set<string>();
30+
const hasVisited = (node: Node) => {
31+
const key = makeKey(node);
32+
if (visited.has(key)) return true;
33+
visited.add(key);
34+
return false;
35+
};
36+
37+
let pointsToVisit: Node[] = [];
38+
MinHeap.push(pointsToVisit, [0, { x: 0, y: 0, steps: 0 }]);
39+
40+
while (pointsToVisit.length > 0) {
41+
let [_, node] = MinHeap.pop(pointsToVisit) as [number, Node];
42+
43+
if (hasVisited(node)) continue;
44+
if (node.y === endY && node.x === endX) {
45+
return node.steps;
46+
}
47+
allDirections.forEach((d) => {
48+
const nextX = node.x + d.dx;
49+
const nextY = node.y + d.dy;
50+
51+
if (
52+
nextY < 0 ||
53+
nextY >= grid.length ||
54+
nextX < 0 ||
55+
nextX >= grid[0].length
56+
)
57+
return;
58+
59+
if (grid[nextY][nextX] === 1) return;
60+
61+
MinHeap.push(pointsToVisit, [
62+
node.steps + 1,
63+
{
64+
x: nextX,
65+
y: nextY,
66+
direction: d,
67+
steps: node.steps + 1,
68+
},
69+
]);
70+
});
71+
}
72+
return -1;
73+
};
74+
75+
const part1 = (rawInput: string) => {
76+
const input = parseInput(rawInput);
77+
78+
const sampleData = input.length < 30;
79+
const endX = input.length < 30 ? 6 : 70;
80+
const endY = input.length < 30 ? 6 : 70;
81+
const grid = new Array(endY + 1)
82+
.fill(0)
83+
.map(() => new Array<number>(endX + 1).fill(0));
84+
85+
input.forEach(([x, y], i) => {
86+
if (i < (sampleData ? 12 : 1024)) {
87+
grid[y][x] = 1;
88+
}
89+
});
90+
91+
return canPath(grid, endX, endY);
92+
};
93+
94+
const part2 = (rawInput: string) => {
95+
const input = parseInput(rawInput);
96+
97+
const sampleData = input.length < 30;
98+
const endX = input.length < 30 ? 6 : 70;
99+
const endY = input.length < 30 ? 6 : 70;
100+
const grid = new Array(endY + 1)
101+
.fill(0)
102+
.map(() => new Array<number>(endX + 1).fill(0));
103+
104+
for (let i = 0; i < input.length; i++) {
105+
let [x, y] = input[i];
106+
if (i < (sampleData ? 12 : 2979)) {
107+
grid[y][x] = 1;
108+
} else {
109+
grid[y][x] = 1;
110+
if (canPath(grid, endX, endY) < 0) {
111+
return `${x},${y}`;
112+
}
113+
}
114+
}
115+
116+
return "passable?";
117+
};
118+
119+
run({
120+
part1: {
121+
tests: [
122+
{
123+
input: `5,4
124+
4,2
125+
4,5
126+
3,0
127+
2,1
128+
6,3
129+
2,4
130+
1,5
131+
0,6
132+
3,3
133+
2,6
134+
5,1
135+
1,2
136+
5,5
137+
2,5
138+
6,5
139+
1,4
140+
0,4
141+
6,4
142+
1,1
143+
6,1
144+
1,0
145+
0,5
146+
1,6
147+
2,0`,
148+
expected: 22,
149+
},
150+
],
151+
solution: part1,
152+
},
153+
part2: {
154+
tests: [
155+
// {
156+
// input: ``,
157+
// expected: "",
158+
// },
159+
],
160+
solution: part2,
161+
},
162+
trimTestInputs: true,
163+
onlyTests: false,
164+
});

0 commit comments

Comments
 (0)