-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michał Kleszczyński
committed
Dec 10, 2024
1 parent
8ee8979
commit 93b836a
Showing
5 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { readFileSync } from 'node:fs' | ||
import { unique } from 'radash' | ||
|
||
import { runExamples, runSolution } from '@magiczne/advent-of-code-ts-core/aoc' | ||
import type { Vec2 } from '@magiczne/advent-of-code-ts-core/types' | ||
|
||
interface Input { | ||
map: ReadonlyArray<ReadonlyArray<number>> | ||
trailheads: ReadonlyArray<Vec2> | ||
} | ||
|
||
// Find every neighbor that is larger than one from current position | ||
const findIncrementedNeighbors = (map: ReadonlyArray<ReadonlyArray<number>>, position: Vec2): ReadonlyArray<Vec2> => { | ||
return [ | ||
{ x: position.x, y: position.y - 1 }, | ||
{ x: position.x, y: position.y + 1 }, | ||
{ x: position.x - 1, y: position.y }, | ||
{ x: position.x + 1, y: position.y }, | ||
].filter(item => { | ||
return map[item.y]?.[item.x] === map[position.y][position.x] + 1 | ||
}) | ||
} | ||
|
||
const part1 = (data: Input): number => { | ||
let score = 0 | ||
|
||
// Searching routes for each trailhead | ||
data.trailheads.forEach(trailhead => { | ||
let positionQueue: Array<Vec2> = [trailhead] | ||
|
||
while (positionQueue.length > 0) { | ||
score += positionQueue.filter(position => data.map[position.y][position.x] === 9).length | ||
|
||
// Removing duplicates that may come from two different nodes in queue - we only need one path in this part of task | ||
positionQueue = unique( | ||
positionQueue.reduce((acc, position) => { | ||
if (data.map[position.y][position.x] === 9) { | ||
return acc | ||
} | ||
|
||
return [...acc, ...findIncrementedNeighbors(data.map, position)] | ||
}, []), | ||
item => `${item.x}${item.y}`, | ||
) | ||
} | ||
}) | ||
|
||
return score | ||
} | ||
|
||
const part2 = (data: Input): number => { | ||
let score = 0 | ||
|
||
// Searching routes for each trailhead | ||
data.trailheads.forEach(trailhead => { | ||
let positionQueue: Array<Vec2> = [trailhead] | ||
|
||
while (positionQueue.length > 0) { | ||
score += positionQueue.filter(position => data.map[position.y][position.x] === 9).length | ||
|
||
positionQueue = positionQueue.reduce<Array<Vec2>>((acc, position) => { | ||
if (data.map[position.y][position.x] === 9) { | ||
return acc | ||
} | ||
|
||
return [...acc, ...findIncrementedNeighbors(data.map, position)] | ||
}, []) | ||
} | ||
}) | ||
|
||
return score | ||
} | ||
|
||
const reader = (file: string): Input => { | ||
const map = readFileSync(file, 'utf-8') | ||
.trim() | ||
.split('\n') | ||
.map(line => { | ||
return line.split('').map(item => parseInt(item, 10)) | ||
}) | ||
|
||
const trailheads = map.flatMap((row, y) => { | ||
return row.reduce<ReadonlyArray<Vec2>>((acc, item, x) => { | ||
if (item !== 0) { | ||
return acc | ||
} | ||
|
||
return [...acc, { x, y }] | ||
}, []) | ||
}) | ||
|
||
return { | ||
map, | ||
trailheads, | ||
} | ||
} | ||
|
||
runExamples(2024, '10', reader, part1, part2) | ||
runSolution(2024, '10', reader, part1, part2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
101672126789567871215432345692104565432149876543210210165210 | ||
234583005412345960104301876783213078985034987034324321894321 | ||
898794112303876450201256969654532123834125870125495696765015 | ||
347105678938989321345347238767651054923456678066780787892196 | ||
256234569821672105496598123698743267812167569876541256543087 | ||
101246541230543236787687034521650167600098452210434347893076 | ||
765487430945650123689874501210010458321899321320345898432125 | ||
812392121876210154318965678342181309456786870451256567589034 | ||
906783032944301269203454369453892211034567965960127655678122 | ||
105498893235692378112103223964763789123998234872198543987901 | ||
012347710145785489023078114875654621001870198101083234896872 | ||
143256601056676521032169005676910538912560187232172105765463 | ||
650101542387234982349850321987823447103451276543432156710354 | ||
781256730498105673256741410212345656458967345210589054801267 | ||
292349821567834984109632565401298765367898721125678963987658 | ||
143218762438921015678541078980345101210787430034567672108349 | ||
012109687921089210010699012899876012101656526540545689019232 | ||
123298796810874391523788743765210023089017017651656567567101 | ||
264567345765965487654345651054382104672108978562347458478210 | ||
876501210054056506961296652567893965543223489478998749309654 | ||
930432011123143215870387743456894876654316554300145632218723 | ||
121098723498234010965436892147765989969807621210239841012212 | ||
032127854567872127830124321038967857878898210345678012332301 | ||
343456965432963236543012430121456546545678149814798763441012 | ||
856547876501454345632101569870367630430789034709823454456787 | ||
987456989982321056567832678971278921821018125616710569344596 | ||
898323678776545963498943267089101012900329870125211678233678 | ||
543214549845034872167654154172112307812234568934303234102189 | ||
670100234012123521015673013263001478703105567078450145221078 | ||
789121124112345677893589234654816589654876432169867276098985 | ||
458012043207654986712432102898987678723997654250178780127676 | ||
367123856978943105403398701787676789210788023443239693234565 | ||
212344987821012312314499652012565478943679117652108012345898 | ||
101055678736703432965488343403454567652543208941032101056789 | ||
567167899645898501876670912212343089801411012432143322765603 | ||
498656018514567610154561801101232156787302347823434410810512 | ||
301565323403078921210432765432320165696213456916565567923443 | ||
212174321012169872321983656430110234345012387107896988019654 | ||
143089102100110165434672106321025693230103896217867899528743 | ||
056345243098241234345543567432234787121212344356956187634454 | ||
167216356784356768766965498589105676034789651243445010546763 | ||
098307458965349889457874321678210987345678760342336729867892 | ||
123478969870221232356301230898121236789860987651029838758981 | ||
874554878561110321001234454567033345652101456787010745643230 | ||
965765467892025412120985213476542101543032321897655678994101 | ||
259854326743476501031276302985433297890125670998556767783210 | ||
108765012654389416542333421874344387743234987123449854654325 | ||
237894121001274327985421510123435674659143498001230123087654 | ||
367123432114565878876430678054510523438032567143456732192123 | ||
458023449323676969216567869067823410547621052232109865434012 | ||
569016558701987854107854952154996564678543121218987774325678 | ||
652345667632234543236943343043287643789431030101876789810089 | ||
421298796545105652345012894332106452100396545012765012102168 | ||
310359687698236701489431765012987367011287236923454324303258 | ||
210541056789127892376320012343456218983470147876565465214549 | ||
323478145632036767665411289854387609322561228945456778927678 | ||
416569230541145678547802398761296543211032310239843899818765 | ||
507858121230978789236921234210435234508943001108702016709654 | ||
698943034567879601105210123456520123677654112345611025212123 | ||
787012347898765432434341038987011038989543233434323134321010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0123 | ||
1234 | ||
8765 | ||
9876 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
89010123 | ||
78121874 | ||
87430965 | ||
96549874 | ||
45678903 | ||
32019012 | ||
01329801 | ||
10456732 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters