From d5bbc8ecdae52ac422f922f9f1fb101758795dda Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 11 Dec 2024 14:29:27 +0000 Subject: [PATCH] Day 10 --- 2024/10/index.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 2024/10/index.ts diff --git a/2024/10/index.ts b/2024/10/index.ts new file mode 100644 index 0000000..a039555 --- /dev/null +++ b/2024/10/index.ts @@ -0,0 +1,62 @@ +const fs = require('fs'); + +const input = fs.readFileSync(__dirname + '/input.txt', 'utf8'); + +interface Coord { x: number, y: number }; + +const topo: Array = input.split("\n").slice(0, -1); + +const height = topo.length; +const width = topo[0].length; + + +let sum = 0; + +function getScore({ x, y }: Coord, height = 0, trail: Array = [], peaks: Array = []): number { + let score = 0; + + const nextHeight = height + 1; + [ + { x, y: y - 1 }, + { x, y: y + 1 }, + { x: x - 1, y }, + { x: x + 1, y }, + ].forEach((coord) => { + if (!topo[coord.y] || !topo[coord.y][coord.x]) { + return; + } + if (parseInt(topo[coord.y][coord.x]) === nextHeight) { + if (nextHeight === 9) { + const coordString = `${coord.x},${coord.y}`; + if (!peaks.includes(coordString)) { + score += 1; + // peaks.push(coordString); // Only for Part 1 + } + } else { + score += getScore(coord, nextHeight, [...trail, { x, y }], peaks); + } + } + }); + + return score; +} + +let y = 0; +let x = 0; +for (let i = 0; i <= input.length; i++) { + const char = input[i]; + if (char === "\n") { + y++; + x = 0; + continue; + } + + if (char === '0') { + const score = getScore({ x, y }); + sum += score; + } + + x++; +} + +console.log(sum);