Skip to content

Commit e91cce2

Browse files
committed
Add solutions to day 10
1 parent 5b33ba3 commit e91cce2

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

src/day10/day10.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as fs from "node:fs/promises";
2+
import { suite, test } from "node:test";
3+
4+
import { part1, part2 } from "./day10.ts";
5+
6+
const [INPUT, INPUT_TEST] = await Promise.all([
7+
fs.readFile(new URL("input.txt", import.meta.url), "utf-8"),
8+
fs.readFile(new URL("input-test.txt", import.meta.url), "utf-8"),
9+
]);
10+
11+
suite("day10", () => {
12+
test("part1", (t) => {
13+
t.assert.equal(part1(INPUT_TEST), 36);
14+
t.assert.equal(part1(INPUT), 629);
15+
});
16+
17+
test("part2", (t) => {
18+
t.assert.equal(part2(INPUT_TEST), 81);
19+
t.assert.equal(part2(INPUT), 1242);
20+
});
21+
});

src/day10/day10.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { lines } from "../utils.ts";
2+
3+
function trails(input: string): IteratorObject<IteratorObject<number>> {
4+
const map = lines(input).flatMap(Iterator.from).map(Number).toArray();
5+
6+
const size = Math.sqrt(map.length);
7+
8+
if (!Number.isInteger(size)) {
9+
throw new TypeError("Not a square!");
10+
}
11+
12+
function* endPositions(
13+
pos: number,
14+
expectedValue: number,
15+
): Generator<number> {
16+
const value = map[pos]!;
17+
18+
if (value !== expectedValue) {
19+
return;
20+
}
21+
22+
if (value === 9) {
23+
yield pos;
24+
return;
25+
}
26+
27+
const x = pos % size;
28+
const y = (pos - x) / size;
29+
30+
if (0 <= x - 1) {
31+
yield* endPositions(size * y + (x - 1), value + 1);
32+
}
33+
34+
if (0 <= y - 1) {
35+
yield* endPositions(size * (y - 1) + x, value + 1);
36+
}
37+
38+
if (x + 1 < size) {
39+
yield* endPositions(size * y + (x + 1), value + 1);
40+
}
41+
42+
if (y + 1 < size) {
43+
yield* endPositions(size * (y + 1) + x, value + 1);
44+
}
45+
}
46+
47+
return map
48+
.entries()
49+
.filter(([_pos, value]) => value === 0)
50+
.map(([pos, _value]) => endPositions(pos, 0));
51+
}
52+
53+
export function part1(input: string): number {
54+
return trails(input)
55+
.map((trail) => new Set(trail).size)
56+
.reduce((a, b) => a + b, 0);
57+
}
58+
59+
export function part2(input: string): number {
60+
return trails(input)
61+
.map((trail) => trail.toArray().length)
62+
.reduce((a, b) => a + b, 0);
63+
}

src/day10/input-test.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
89010123
2+
78121874
3+
87430965
4+
96549874
5+
45678903
6+
32019012
7+
01329801
8+
10456732

src/day10/input.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
56760198543405456770107687012987873265430701234123
2+
47891087612514320889298596543236982176321878985054
3+
30932912503423411978321436760145453089490965076567
4+
21043803498598502565450345897896334569584324105498
5+
43256754387687643445601231056787276578675013212389
6+
50105620896014553238782322348996189432106522301210
7+
60234211205423469109695210567345023456987101232569
8+
71454301312345678100556501051269010787832198543478
9+
82365985407856743291447892340178732696949037643323
10+
99878876556989856782332309650065341054658743252110
11+
12389565445845698943221218761234456565789650167087
12+
03458890330532787892100321052442787074656782108996
13+
12766721221621056521098478934321692183245891210105
14+
00875437876521245430167569035630013290194300121234
15+
21980356985430130101456122128754324389980215432101
16+
32341245234561221212340033439961015671271326990123
17+
41239832101676678703121542345872343760362347887654
18+
50146701098787569254035671096112654854454558943210
19+
69655410889693452164549389987003569943003967656787
20+
78789324989582543076678432176124578752112876545490
21+
49670123476451001289986581065437669801198743454321
22+
34565404560302654345677893034458954321034652345210
23+
21670313401212701238766732123361056700125601216787
24+
10781223304343870349676678901272341811098700305891
25+
87690433210154901234587565078980110925643212456730
26+
98521049873269100448996652169765223434756965569821
27+
83430656794378234567385543258894304589807874321034
28+
12345690185123247855434789043210113676212981012125
29+
04396783276030110982329872178901923494345891234596
30+
65287654896543225671012763561032876587436780989687
31+
70132108987652334430101654432945214306525891078765
32+
89945345456701498521012346547876305211014342569854
33+
67876276789876567677890107236521456523210213410743
34+
56940189678930438988743298101430567894765104323212
35+
45434328509821321089654340122334567765894101298701
36+
50125613410030012127763019831021998568903210345692
37+
23498701322147897898892123742340867478912653210789
38+
10567654213456786721089054654356789302801743105678
39+
01678123402110995437658766789210676211010892234109
40+
67569016567021874378941043212101245432346781103201
41+
58454323898134565632332456701234306011015490321232
42+
49323454234012906701454327890965412102367305410147
43+
31012365125643812898765410378876543233458216568758
44+
21009876034756763019034101269989100146569325679669
45+
21898745129829654323129654352123299656478012784578
46+
30743231065018760563238765543034788798329983693056
47+
45656189654323011056769894672145632347012674212147
48+
01218088745896522349850123283034701456981065100238
49+
14309893236787631438943210190129892123672578921109
50+
25456782101896540127654301012010181034543465433212

0 commit comments

Comments
 (0)