Skip to content

Commit 1202cdf

Browse files
committed
Year 2024, Day 10
1 parent 3a465a8 commit 1202cdf

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

clients/typescript/solutions/S2410.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import ISolution from "./ISolution.ts";
2+
3+
export default class S2410 implements ISolution {
4+
firstPart(input: string): (number | string) | Promise<number | string> {
5+
const numbers = input.split("\n").map(line => line.split("").map(Number));
6+
const zeros = numbers.map((line, y) => line.map((a, x) => [ a, x ]).filter(a => a[ 0 ] === 0).map(a => [ a[ 1 ], y ])).flat();
7+
return zeros.map(zero =>
8+
this.step([ zero[ 0 ], zero[ 1 ] ], 1, numbers)
9+
.filter((x, i, arr) => arr.findIndex(y => y[ 0 ] === x[ 0 ] && y[ 1 ] === x[ 1 ]) === i).length
10+
).reduce((acc, val) => acc + val, 0);
11+
}
12+
secondPart(input: string): (number | string) | Promise<number | string> {
13+
const numbers = input.split("\n").map(line => line.split("").map(Number));
14+
const zeros = numbers.map((line, y) => line.map((a, x) => [ a, x ]).filter(a => a[ 0 ] === 0).map(a => [ a[ 1 ], y ])).flat();
15+
return zeros.map(zero =>
16+
this.step([ zero[ 0 ], zero[ 1 ] ], 1, numbers).length
17+
).reduce((acc, val) => acc + val, 0);
18+
}
19+
20+
step(pos: [ number, number ], number: number, numbers: number[][]): [ number, number ][] {
21+
if (number === 10) {
22+
return [ pos ];
23+
}
24+
return [
25+
pos[ 1 ] - 1 >= 0 ? [ pos[ 0 ], pos[ 1 ] - 1, numbers[ pos[ 1 ] - 1 ][ pos[ 0 ] ] ] : [ -1, -1, -1 ],
26+
pos[ 1 ] + 1 < numbers.length ? [ pos[ 0 ], pos[ 1 ] + 1, numbers[ pos[ 1 ] + 1 ][ pos[ 0 ] ] ] : [ -1, -1, -1 ],
27+
pos[ 0 ] - 1 >= 0 ? [ pos[ 0 ] - 1, pos[ 1 ], numbers[ pos[ 1 ] ][ pos[ 0 ] - 1 ] ] : [ -1, -1, -1 ],
28+
pos[ 0 ] + 1 < numbers[ 0 ].length ? [ pos[ 0 ] + 1, pos[ 1 ], numbers[ pos[ 1 ] ][ pos[ 0 ] + 1 ] ] : [ -1, -1, -1 ]
29+
]
30+
.filter(x => x[ 2 ] === number)
31+
.map(x => this.step([ x[ 0 ], x[ 1 ] ], number + 1, numbers))
32+
.flat();
33+
}
34+
35+
}

0 commit comments

Comments
 (0)