Skip to content

Commit 5e1bba8

Browse files
chore(ts): 2024/d6/p1
1 parent fb00624 commit 5e1bba8

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## My AOC Stats
22

3-
Total stars: **28**
3+
Total stars: **29**
44

55
| Day | [2015][link-2015] | [2016][link-2016] | [2017][link-2017] | [2018][link-2018] | [2019][link-2019] | [2020][link-2020] | [2021][link-2021] | [2022][link-2022] | [2023][link-2023] | [2024][link-2024] |
66
| :--------: | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- |
@@ -9,7 +9,7 @@ Total stars: **28**
99
| **3** | | | | | | | | | ⭐⭐ | ⭐⭐ |
1010
| **4** | ⭐⭐ | | | | | | | | ⭐⭐ | ⭐⭐ |
1111
| **5** | | | | | | | | || ⭐⭐ |
12-
| **6** | | | | | | | | | ⭐⭐ | |
12+
| **6** | | | | | | | | | ⭐⭐ | |
1313
| **7** | | | | | | | | | | |
1414
| **8** | | | | | | | | || |
1515
| **9** | | | | | | | | | | |
@@ -29,7 +29,7 @@ Total stars: **28**
2929
| **23** | | | | | | | | | | |
3030
| **24** | | | | | | | | | | |
3131
| **25** | | | | | | | | | | |
32-
| **Total:** | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 12 | 10 |
32+
| **Total:** | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 12 | 11 |
3333

3434
[link-2015]: https://github.com/ShubhamVerma1811/advent-of-code/tree/main/src/2015
3535
[link-2016]: https://github.com/ShubhamVerma1811/advent-of-code/tree/main/src/2016

src/2024/day-06/typescript/index.ts

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { readFile } from 'node:fs/promises';
2+
import { resolve } from 'node:path';
3+
4+
async function main() {
5+
const data = await readFile(
6+
resolve('../../../../', 'data', '2024-06.txt'),
7+
'utf8'
8+
);
9+
10+
// Part 1
11+
{
12+
console.time('Part 1');
13+
const result = part1(data);
14+
console.info('Result:', result);
15+
console.timeEnd('Part 1');
16+
}
17+
18+
// Part 2
19+
{
20+
console.time('Part 2');
21+
const result = part2(data);
22+
console.info('Result:', result);
23+
console.timeEnd('Part 2');
24+
}
25+
}
26+
27+
main();
28+
29+
export function part1(data: string) {
30+
const lines = data.trim().split('\n');
31+
// @ts-ignore
32+
let guardPos: {
33+
x: number;
34+
y: number;
35+
direction: 'up' | 'down' | 'left' | 'right';
36+
} = {};
37+
38+
let sum = 0;
39+
const seen = new Set();
40+
41+
const grid = lines.map((line, rowIdx) => {
42+
return line.split('').map((col, colIdx) => {
43+
if (col === '^') {
44+
guardPos = {
45+
x: colIdx,
46+
y: rowIdx,
47+
direction: 'up',
48+
};
49+
}
50+
return col;
51+
});
52+
});
53+
54+
seen.add(`${guardPos.x}-${guardPos.y}`);
55+
sum += 1;
56+
57+
while (true) {
58+
if (
59+
guardPos.y < 0 ||
60+
guardPos.y >= grid.length ||
61+
guardPos.x < 0 ||
62+
guardPos.x >= grid[0].length
63+
) {
64+
break;
65+
}
66+
67+
switch (guardPos.direction) {
68+
case 'up':
69+
if (grid?.[guardPos.y - 1]?.[guardPos.x] !== '#') {
70+
guardPos.y -= 1;
71+
72+
if (!seen.has(`${guardPos.x}-${guardPos.y}`)) {
73+
seen.add(`${guardPos.x}-${guardPos.y}`);
74+
sum += 1;
75+
}
76+
} else {
77+
guardPos.direction = 'right';
78+
}
79+
break;
80+
case 'right':
81+
if (grid?.[guardPos.y]?.[guardPos.x + 1] !== '#') {
82+
guardPos.x += 1;
83+
84+
if (!seen.has(`${guardPos.x}-${guardPos.y}`)) {
85+
seen.add(`${guardPos.x}-${guardPos.y}`);
86+
sum += 1;
87+
}
88+
} else {
89+
guardPos.direction = 'down';
90+
}
91+
break;
92+
case 'down':
93+
if (grid?.[guardPos.y + 1]?.[guardPos.x] !== '#') {
94+
guardPos.y += 1;
95+
96+
if (!seen.has(`${guardPos.x}-${guardPos.y}`)) {
97+
seen.add(`${guardPos.x}-${guardPos.y}`);
98+
sum += 1;
99+
}
100+
} else {
101+
guardPos.direction = 'left';
102+
}
103+
break;
104+
case 'left':
105+
if (grid?.[guardPos.y]?.[guardPos.x - 1] !== '#') {
106+
guardPos.x -= 1;
107+
108+
if (!seen.has(`${guardPos.x}-${guardPos.y}`)) {
109+
seen.add(`${guardPos.x}-${guardPos.y}`);
110+
sum += 1;
111+
}
112+
} else {
113+
guardPos.direction = 'up';
114+
}
115+
break;
116+
}
117+
}
118+
119+
return sum - 1;
120+
}
121+
122+
export function part2(data: string) {}

0 commit comments

Comments
 (0)