Skip to content

Commit

Permalink
2024 day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
ghbuck committed Dec 10, 2024
1 parent 653f585 commit 297a97c
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/2024/10/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Point, RunParams, Solution } from 'utils/dataTypes/index.js'
import { getInput } from 'utils/files/index.js'
import { printAnswers } from 'utils/printing/index.js'

interface PathInfo {
endpoints: Set<string>
rating: number
}

interface PathValuation {
score: number
rating: number
}

const followPath = async (value: number, endValue: number, point: Point, map: number[][]): Promise<PathInfo> => {
const pathInfo: PathInfo = {
endpoints: new Set<string>(),
rating: 0,
}

const nextValue = value + 1
const nextOptions: Point[] = [
{
x: point.x,
y: point.y - 1,
},
{
x: point.x + 1,
y: point.y,
},
{
x: point.x,
y: point.y + 1,
},
{
x: point.x - 1,
y: point.y,
},
].filter((option: Point) => map[option.y]?.[option.x] === nextValue)

if (nextValue === endValue) {
pathInfo.rating = nextOptions.length
for (const option of nextOptions) {
pathInfo.endpoints.add(`${option.x},${option.y}`)
}
} else {
for (const option of nextOptions) {
const furtherDownThePathInfo = await followPath(nextValue, endValue, option, map)

pathInfo.rating += furtherDownThePathInfo.rating
for (const option of furtherDownThePathInfo.endpoints) {
pathInfo.endpoints.add(option)
}
}
}

return pathInfo
}

const evaluateTrails = async (map: number[][], trailEnd: number): Promise<PathValuation> => {
const zeros: Point[] = []

for (let rowIndex = 0; rowIndex < map.length; ++rowIndex) {
const row = map[rowIndex]

for (let colIndex = 0; colIndex < row.length; ++colIndex) {
const col = row[colIndex]

if (col === 0) {
zeros.push({ x: colIndex, y: rowIndex })
}
}
}

return Promise.all(zeros.map((point: Point) => followPath(0, trailEnd, point, map))).then((values: PathInfo[]) => {
return {
score: values.reduce((total: number, current: PathInfo) => (total += current.endpoints.size), 0),
rating: values.reduce((total: number, current: PathInfo) => (total += current.rating), 0),
}
})
}

export const run = async (params: RunParams) => {
const solution: Solution = {
part1: params.isTest ? 36 : 459,
part2: params.isTest ? 81 : 1034,
}

const input = getInput(params)
.split('\n')
.map((row: string) => row.split('').map(Number))

const trailValues = await evaluateTrails(input, 9)

printAnswers({
params,
answer1: trailValues.score,
answer2: trailValues.rating,
solution,
})
}
40 changes: 40 additions & 0 deletions src/2024/10/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
1098921121187650126589432104301010017898
2347810030090543237676546765218921326323
3256723345121789078545345896237635495410
0189654656230650129454210910146546786898
1018706787649843212323407893056544576781
0123215498556764501012216324567033675410
1054912389440189650983345413498122189323
2367804322339218761874214102439232075014
3494565011878307010965302101521001456985
4583876910965498123434456517617652327876
9672978894328767894525467898908543410434
8701569765419456987616321010119654306523
7616054100300345865407890121236701217810
4567123231201210870301456290547896332912
3258834998303456961210387787678987441003
4109985867214327898341295689432196556764
3457876754321016987654254776501001105895
2568965698130123216510163897567232234996
1077654147010154105425672198498143497887
2089503056923269012334789010398056786546
1123412147874678901109011001267049805430
0109892130965165210278921123452121012321
1236783021089014321367630038983430328901
0345634569870156752456541127604589437610
1267825478763247843898430934510678576523
3216910089654130956707321874321987689430
4505432198703021013210012365899654238321
3699801789012982787309898456718723148980
2789789678101276896456721032100210057671
1008650521010345785454434549321321060362
2210541430121289890365410678732639871250
4341232510537656701274320521548747898341
1056341423498545432789201230699656743432
2967650345567230101687112345788745234569
3878981236750121211096001296787230199678
4589870109889032349125410187590123288767
5679665010976541498934231095691054177678
3038754129889650587432145654782567065549
2125603236778765676501098723123478450030
3034512345654656565410181010010989321121
8 changes: 8 additions & 0 deletions src/2024/10/testInput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732

0 comments on commit 297a97c

Please sign in to comment.