Skip to content

Commit 18d1ae2

Browse files
committed
2024 day 10
1 parent 3b9d091 commit 18d1ae2

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

2024/input/10.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
981050112210121034565432321032103321234567567
2+
872341201014434921878891434549012310432198678
3+
765434318123567890989760023678543986501021549
4+
545985329854356792105654116707621677893430932
5+
456576416763447883498743209811030543082567801
6+
327676509823430976541050124322345892121078998
7+
218489678016521050132011267401276734289010867
8+
109345908987210763249121278569889825678123459
9+
543237817876345854358230989078778910107645678
10+
654106726541096981267345678123498723216530501
11+
761235430432387870301076521014567234345321432
12+
890144321001456765432189410019010121237876501
13+
781055697878921212345674321928723210345989432
14+
218966788965430101254789345839654234326710101
15+
109875692100123270163231236743010165019823321
16+
018754103018786789872100109852123278956754430
17+
325673214109695698987212121961094109849869543
18+
234981005234534321210103030878785603732778672
19+
189012346943413210321234549879676712011001981
20+
076321057892100110450321676566543893425652760
21+
325401069787012026565410789457012798534743854
22+
410512178976543237656998898308932687649887923
23+
567893467689854148997867891219801456308996310
24+
234554992501763056788950750304762343210105409
25+
147667881432612234563441065403451012308712358
26+
018589670109803189612532674312332305419654347
27+
189478521210789078703675689100123498569323256
28+
789767432345650876589986789210096567878012100
29+
679854343217821987676521654323487458901223321
30+
565943034506934910505430598714510329874341234
31+
450012129645445823412014567609621210965210585
32+
321101518789336798703123455418760145670195696
33+
234965400689221209654328954303210234987784787
34+
105874321678100118765017763214301001056653610
35+
876701234549010329870126894505212892347542123
36+
965891098238321234561235214676703083498230036
37+
874782347107630145490344305987894176580121545
38+
923690456016549854987432234589765237891234694
39+
012541219897896768876501105676894396790346787
40+
001432108766541089004321212988787781687656698
41+
123498789655432376113410145679695670521044567
42+
876567610141045645223567034034544321430123698
43+
965458901232038764344038123129632487654310432
44+
012307676540129854965129876038701898943216581
45+
103212789432100123876434565345612781012107890

2024/js/day10.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { asLines } from './util.js';
2+
3+
const data = asLines('../input/10.txt').map(l => l.split("").map(h => Number(h)));
4+
5+
const trailHeads = data.flatMap((r, y) => r.map((h, x) => [h, x, y]).filter(([h]) => h == 0));
6+
7+
const moves = [[0, 1], [0, -1], [1, 0], [-1, 0]];
8+
9+
const score = ([_, x, y], type = 1) => {
10+
const visited = new Set();
11+
let score = 0;
12+
const queue = [[x, y]];
13+
while(queue.length > 0) {
14+
const [x, y] = queue.shift();
15+
if (visited.has(`${x}.${y}`) && type == 1) continue;
16+
visited.add(`${x}.${y}`);
17+
const height = data[y][x];
18+
if (height >= 9) {
19+
score++;
20+
} else {
21+
const nextPositions = moves.map(([dx, dy]) => [x + dx, y + dy])
22+
.filter(([x, y]) => x >= 0 && y >= 0 && x < data[0].length && y < data.length) // inside map
23+
.filter(([x, y]) => data[y][x] == height + 1) // one higher
24+
queue.push(...nextPositions);
25+
}
26+
}
27+
return score;
28+
};
29+
30+
const scores = trailHeads.map(tr => score(tr));
31+
const sum = scores.reduce((a,b) => a + b);
32+
33+
console.log(`part1: ${sum}`);
34+
35+
const scores2 = trailHeads.map(tr => score(tr, 2));
36+
const sum2 = scores2.reduce((a,b) => a + b);
37+
38+
console.log(`part2: ${sum2}`);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy