Compare commits
2 commits
314c6215f3
...
ed08211f46
Author | SHA1 | Date | |
---|---|---|---|
tristan | ed08211f46 | ||
tristan | db212d5bda |
8
2024/10/example.txt
Normal file
8
2024/10/example.txt
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
89010123
|
||||||
|
78121874
|
||||||
|
87430965
|
||||||
|
96549874
|
||||||
|
45678903
|
||||||
|
32019012
|
||||||
|
01329801
|
||||||
|
10456732
|
58
2024/10/input.txt
Normal file
58
2024/10/input.txt
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
5678970120787667809876787651450321789810165432234561012345
|
||||||
|
4301787431296556912765698540341410786728765501103676545434
|
||||||
|
3212896594365443213454501231232545695439154610898387656946
|
||||||
|
4307885785872337801653215432545694321089043781763296047877
|
||||||
|
5456934576901236998740126721694787899676112891054102137898
|
||||||
|
4327825676210365485035432830780146788765208982567873223703
|
||||||
|
1012010789301345304126701910567235623654367643432984012612
|
||||||
|
9887652105401253213239878323458945514545106543221265698543
|
||||||
|
6798943766798760560145569850179876408763215456100896787432
|
||||||
|
5212237854899621056776457763287654309854580367018701656501
|
||||||
|
4302108983014552345889308954390101218345691278929632540987
|
||||||
|
8921321212123467496973217654321010101210782987834541231236
|
||||||
|
7010450908765658787210106563898110567623458906543210340145
|
||||||
|
6524567849434349872323293478967223408988967217890107459054
|
||||||
|
5433008956721238721494782566554310510177654394345498768765
|
||||||
|
8942112349810101430585691057431214321287656783216321659056
|
||||||
|
9853523658901233549674541008120109450392345654307010123141
|
||||||
|
6765434567890312678234432219078218765431874309458927034230
|
||||||
|
1034323450765403510165498348569341016210967218567898985541
|
||||||
|
4125614321877654523276327653414452547893458967898769876632
|
||||||
|
3210701234988347678987014512103963458982105450745601896781
|
||||||
|
4678890215679298689898101105432878967821123301234312765890
|
||||||
|
5469810309100198776543239416001263456710054210126543454323
|
||||||
|
6954323458210789743987678327122452349821269329237632670110
|
||||||
|
7856542167345679812310565498214301265430178778748911789224
|
||||||
|
3067630018901256701423457012303210178923476565652100654343
|
||||||
|
2188921078872345690501298989452121089012383418983434334534
|
||||||
|
3298934569863418987632367898763011298234592307894521025676
|
||||||
|
0387650101678507876753456501014980347105681016765601210787
|
||||||
|
1456343212589216909865401432325671256501789823454782309898
|
||||||
|
2341067823410365419872316543234560787432328987123495434321
|
||||||
|
8932058934321474321891027652101765698543212656016596521030
|
||||||
|
7634149965430589890765438984989854321692303443067787676543
|
||||||
|
4543232876787672763210567823870143430781054512198971980612
|
||||||
|
4687601045298101454569498014561034231278766503456890121701
|
||||||
|
3894523432101212343278307601432120140389107652107765439890
|
||||||
|
2183410589043239852101212587347899655473298940998987610141
|
||||||
|
1012398679650126760120103496256978796554567831876898543234
|
||||||
|
0310487778743245679833210145107878987143278722365687650125
|
||||||
|
1223456899012632988744103230123217610012189013451232105676
|
||||||
|
8346543456598701279655654389874306526323076567600345694789
|
||||||
|
9857812347405654210346969210165435435414105458912256783238
|
||||||
|
6768901098312343981237878301456521056905912345863109890104
|
||||||
|
0345650101232107834369765412347678167876801056874223454323
|
||||||
|
1278761321943456125078098943678999101210760767985214567910
|
||||||
|
2109874430854534076165107834567783212323458898876307698876
|
||||||
|
3436543561763325689234256623478654323212789954343298714565
|
||||||
|
4567612675610118763240345510569823434101652765210134503443
|
||||||
|
5698203984323709454121245432234712345612501897898325612652
|
||||||
|
6782100112345890365039876101165601016780432101107210726761
|
||||||
|
6783078201076761276321276543036523239891569232216874835890
|
||||||
|
5894569345987457889430389236543210145652678740125965934701
|
||||||
|
6784578896590356996321298107012301276743245656734014821012
|
||||||
|
5693678787101243987654301058905434985890130543876523498763
|
||||||
|
4542109843262012276019012765676125673981021982923434549854
|
||||||
|
3432101257876540145328943894387089012832123671019323676345
|
||||||
|
4309210369901234239457654703298976326721034501208710789234
|
||||||
|
3218765478710123378765645612107845435434345212345621678101
|
55
2024/10/solution.js
Normal file
55
2024/10/solution.js
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
|
||||||
|
const input = (await Bun.file("input.txt").text()).trim()
|
||||||
|
|
||||||
|
const rows = [[]];
|
||||||
|
|
||||||
|
const trailheads = [];
|
||||||
|
|
||||||
|
for (let i = 0, x = 0, y = 0; i < input.length; i++) {
|
||||||
|
if (input[i] == "\n") {
|
||||||
|
rows.push([]);
|
||||||
|
y++;
|
||||||
|
x = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const alt = parseInt(input[i]);
|
||||||
|
rows[y].push(alt);
|
||||||
|
if (alt == 0) {
|
||||||
|
trailheads.push({x,y})
|
||||||
|
}
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
|
||||||
|
const summitChart = Array.from({length: rows.length}, () => Array.from({length: rows.length}, () => (new Set())))
|
||||||
|
|
||||||
|
function findSummits({x, y}) {
|
||||||
|
if (summitChart[y][x].size > 0) {
|
||||||
|
return summitChart[y][x]
|
||||||
|
}
|
||||||
|
if (rows[y][x] == 9) {
|
||||||
|
summitChart[y][x].add(x + y * rows.length)
|
||||||
|
return summitChart[y][x]
|
||||||
|
}
|
||||||
|
if (x != 0 && rows[y][x-1] - rows[y][x] == 1) {
|
||||||
|
summitChart[y][x] = summitChart[y][x].union(findSummits({y, x: x - 1}))
|
||||||
|
}
|
||||||
|
if (x != rows.length - 1 && rows[y][x+1] - rows[y][x] == 1) {
|
||||||
|
summitChart[y][x] = summitChart[y][x].union(findSummits({y, x: x + 1}))
|
||||||
|
}
|
||||||
|
if (y != 0 && rows[y-1][x] - rows[y][x] == 1) {
|
||||||
|
summitChart[y][x] = summitChart[y][x].union(findSummits({y: y-1, x}))
|
||||||
|
}
|
||||||
|
if (y != rows.length - 1 && rows[y+1][x] - rows[y][x] == 1) {
|
||||||
|
summitChart[y][x] = summitChart[y][x].union(findSummits({y: y+1, x}))
|
||||||
|
}
|
||||||
|
return summitChart[y][x]
|
||||||
|
}
|
||||||
|
|
||||||
|
let sum = 0;
|
||||||
|
for (const trailhead of trailheads) {
|
||||||
|
const summits = findSummits(trailhead)
|
||||||
|
sum += summits.size
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(sum)
|
||||||
|
|
55
2024/10/solution2.js
Normal file
55
2024/10/solution2.js
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
|
||||||
|
const input = (await Bun.file("input.txt").text()).trim()
|
||||||
|
|
||||||
|
const rows = [[]];
|
||||||
|
|
||||||
|
const trailheads = [];
|
||||||
|
|
||||||
|
for (let i = 0, x = 0, y = 0; i < input.length; i++) {
|
||||||
|
if (input[i] == "\n") {
|
||||||
|
rows.push([]);
|
||||||
|
y++;
|
||||||
|
x = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const alt = parseInt(input[i]);
|
||||||
|
rows[y].push(alt);
|
||||||
|
if (alt == 0) {
|
||||||
|
trailheads.push({x,y})
|
||||||
|
}
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
|
||||||
|
const summitChart = Array.from({length: rows.length}, () => Array.from({length: rows.length}, () => ([])))
|
||||||
|
|
||||||
|
function findSummits({x, y}) {
|
||||||
|
if (summitChart[y][x].length > 0) {
|
||||||
|
return summitChart[y][x]
|
||||||
|
}
|
||||||
|
if (rows[y][x] == 9) {
|
||||||
|
summitChart[y][x].push(x + y * rows.length)
|
||||||
|
return summitChart[y][x]
|
||||||
|
}
|
||||||
|
if (x != 0 && rows[y][x-1] - rows[y][x] == 1) {
|
||||||
|
summitChart[y][x].push(...findSummits({y, x: x - 1}))
|
||||||
|
}
|
||||||
|
if (x != rows.length - 1 && rows[y][x+1] - rows[y][x] == 1) {
|
||||||
|
summitChart[y][x].push(...findSummits({y, x: x + 1}))
|
||||||
|
}
|
||||||
|
if (y != 0 && rows[y-1][x] - rows[y][x] == 1) {
|
||||||
|
summitChart[y][x].push(...findSummits({y: y-1, x}))
|
||||||
|
}
|
||||||
|
if (y != rows.length - 1 && rows[y+1][x] - rows[y][x] == 1) {
|
||||||
|
summitChart[y][x].push(...findSummits({y: y+1, x}))
|
||||||
|
}
|
||||||
|
return summitChart[y][x]
|
||||||
|
}
|
||||||
|
|
||||||
|
let sum = 0;
|
||||||
|
for (const trailhead of trailheads) {
|
||||||
|
const summits = findSummits(trailhead)
|
||||||
|
sum += summits.length
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(sum)
|
||||||
|
|
Loading…
Reference in a new issue