Skip to content

Commit 52ba6fb

Browse files
chore(ts): 2024/d4
1 parent 1175933 commit 52ba6fb

File tree

3 files changed

+136
-6
lines changed

3 files changed

+136
-6
lines changed

README.md

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

3-
Total stars: **23**
3+
Total stars: **26**
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
| :--------: | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- | :---------------- |
77
| **1** | ⭐⭐ | | | | | | | | ⭐⭐ | ⭐⭐ |
8-
| **2** | ⭐⭐ | | | | | | | | ⭐⭐ | |
8+
| **2** | ⭐⭐ | | | | | | | | ⭐⭐ | |
99
| **3** | | | | | | | | | ⭐⭐ | ⭐⭐ |
10-
| **4** | ⭐⭐ | | | | | | | | ⭐⭐ | |
10+
| **4** | ⭐⭐ | | | | | | | | ⭐⭐ | ⭐⭐ |
1111
| **5** | | | | | | | | || |
1212
| **6** | | | | | | | | | ⭐⭐ | |
1313
| **7** | | | | | | | | | | |
@@ -29,7 +29,7 @@ Total stars: **23**
2929
| **23** | | | | | | | | | | |
3030
| **24** | | | | | | | | | | |
3131
| **25** | | | | | | | | | | |
32-
| **Total:** | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 12 | 5 |
32+
| **Total:** | 6 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 12 | 8 |
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

biome.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
"linter": {
1616
"enabled": true,
1717
"rules": {
18-
"recommended": true
18+
"recommended": true,
19+
"style": {
20+
"noParameterAssign": "warn"
21+
}
1922
}
2023
},
2124
"organizeImports": {
2225
"enabled": true
2326
}
24-
}
27+
}

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

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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-04.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 matrix = data
31+
.trim()
32+
.split('\n')
33+
.map((line) => line.split(''));
34+
35+
let sum = 0;
36+
37+
for (let rowIdx = 0; rowIdx < matrix.length; rowIdx++) {
38+
for (let cellIdx = 0; cellIdx < matrix[rowIdx].length; cellIdx++) {
39+
const top = getTop(matrix, rowIdx, cellIdx).join('');
40+
const left = getLeft(matrix, rowIdx, cellIdx).join('');
41+
const topLeft = getTopLeft(matrix, rowIdx, cellIdx).join('');
42+
const topRight = getTopRight(matrix, rowIdx, cellIdx).join('');
43+
44+
if (top === 'XMAS' || top === 'SAMX') {
45+
sum += 1;
46+
}
47+
if (left === 'XMAS' || left === 'SAMX') {
48+
sum += 1;
49+
}
50+
if (topLeft === 'XMAS' || topLeft === 'SAMX') {
51+
sum += 1;
52+
}
53+
if (topRight === 'XMAS' || topRight === 'SAMX') {
54+
sum += 1;
55+
}
56+
}
57+
}
58+
59+
return sum;
60+
}
61+
62+
export function part2(data: string) {
63+
const matrix = data
64+
.trim()
65+
.split('\n')
66+
.map((line) => line.split(''));
67+
68+
let sum = 0;
69+
70+
for (let rowIdx = 0; rowIdx < matrix.length; rowIdx++) {
71+
for (let cellIdx = 0; cellIdx < matrix[rowIdx].length; cellIdx++) {
72+
const c = matrix[rowIdx][cellIdx];
73+
74+
if (c === 'A') {
75+
const topLeft = matrix[rowIdx - 1]?.[cellIdx - 1];
76+
const topRight = matrix[rowIdx - 1]?.[cellIdx + 1];
77+
const bottomLeft = matrix[rowIdx + 1]?.[cellIdx - 1];
78+
const bottomRight = matrix[rowIdx + 1]?.[cellIdx + 1];
79+
80+
const l = topLeft + c + bottomRight;
81+
const r = topRight + c + bottomLeft;
82+
83+
if ((l === 'MAS' || l === 'SAM') && (r === 'MAS' || r === 'SAM')) {
84+
sum += 1;
85+
}
86+
}
87+
}
88+
}
89+
90+
return sum;
91+
}
92+
93+
function getTop(matrix: string[][], x: number, y: number): string[] {
94+
return [
95+
matrix?.[x]?.[y],
96+
matrix?.[x - 1]?.[y],
97+
matrix?.[x - 2]?.[y],
98+
matrix?.[x - 3]?.[y],
99+
];
100+
}
101+
102+
function getLeft(matrix: string[][], x: number, y: number): string[] {
103+
return [
104+
matrix?.[x]?.[y],
105+
matrix?.[x]?.[y - 1],
106+
matrix?.[x]?.[y - 2],
107+
matrix?.[x]?.[y - 3],
108+
];
109+
}
110+
111+
function getTopLeft(matrix: string[][], x: number, y: number): string[] {
112+
return [
113+
matrix?.[x]?.[y],
114+
matrix?.[x - 1]?.[y - 1],
115+
matrix?.[x - 2]?.[y - 2],
116+
matrix?.[x - 3]?.[y - 3],
117+
];
118+
}
119+
120+
function getTopRight(matrix: string[][], x: number, y: number): string[] {
121+
return [
122+
matrix?.[x]?.[y],
123+
matrix?.[x - 1]?.[y + 1],
124+
matrix?.[x - 2]?.[y + 2],
125+
matrix?.[x - 3]?.[y + 3],
126+
];
127+
}

0 commit comments

Comments
 (0)