From cb1d1fe9787f933d4b51113243bfe0ab26103cea Mon Sep 17 00:00:00 2001 From: monoid Date: Tue, 10 Dec 2024 15:18:33 +0900 Subject: [PATCH] Add solution day 10 --- day_10/example.txt | 8 +++++ day_10/input.txt | 54 ++++++++++++++++++++++++++++++++ day_10/solve_1.ts | 76 ++++++++++++++++++++++++++++++++++++++++++++++ day_10/solve_2.ts | 51 +++++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+) create mode 100644 day_10/example.txt create mode 100644 day_10/input.txt create mode 100644 day_10/solve_1.ts create mode 100644 day_10/solve_2.ts diff --git a/day_10/example.txt b/day_10/example.txt new file mode 100644 index 0000000..7bb1248 --- /dev/null +++ b/day_10/example.txt @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 \ No newline at end of file diff --git a/day_10/input.txt b/day_10/input.txt new file mode 100644 index 0000000..a2ee66b --- /dev/null +++ b/day_10/input.txt @@ -0,0 +1,54 @@ +543067650323210321032110356789890110710189878760134567 +612148941212306782345091235410765227893258759651021298 +701238932301456798106787549323454336794567549842330807 +898547215450589867287654678892961245987654456732498910 +987656306769678956398013234781870301256003301201567821 +234543215878760145478920105690210982340112210341056932 +109650124981232230569834124567345673451234985452347845 +018744323890341021656765033438454589298545676701037796 +199803210787650110765017842129823672107655677812378987 +785012345654789541874326956010510563201234389983405676 +174321498723498632903455467173421454102348210234514430 +065430239014323721212762398982567876218959650149623521 +145540128765017890101871081071018967327968743898734678 +236692134984178768718981012567894458456876212798604329 +987783005673269659654108923458763349663212108789015012 +896654012562154546743267830309854218767103419276126787 +765780123473043432890154321212903109878756578125435698 +434099874984012301740125654567812893469017647030165556 +323101065765101787434436763058906732154178736543256765 +013298187890678696325569892143215440043289128954343894 +010367296541549045016678765014300351234567037760012983 +329458305032132134787867410967891267653438546891898012 +458789414145010120196986323898763298344129655432743210 +567076523236678743285898456777654101201098766653658901 +456167654107549658374567689985403100312434985567877014 +343228943298938569843210576876312238920125676656986323 +651014543498127478755765435410278947437876985543235434 +743223672567078369876890324320127656506987234350145632 +894102981089569232125981210451256543215432122341076541 +765091234676352101034674323401237894101341001456789830 +014780365445443032126501012532345743103210112565898321 +123689876034321145098432107645236743254101101876543210 +094545763125210056788943258896107858969010332987674301 +785430432106782169877652349787567967678321245696784561 +876021569045893458965601654343478914565414326781093870 +987109878010432167014298710234323901258905015492012901 +789219894324569052123109620123217892109876012323458692 +654308765543678743013234538994106543010870965410069783 +789877787632189852101110145885287676521961871012178654 +034565896601087601211001406776896789437652676523468743 +121053795542194512389132312345445676548943989430549012 +262342187634765433498545434531230107401232104321678298 +876533014123892324567696526540043298340981010780890187 +983414523016101019876587017832134781256672125690743296 +032101654327892100345678789901295690764543434321652345 +123089789236743981234589654800387789835678987787601336 +854176590187656876104310343211456876544549016098543223 +965765410194545675465231230898565963233432145123210112 +876894321243232980370110141767653210112321039654101101 +146765232100101061289879852632154100203410128789210322 +036787103761267876518768763543069911012543454376325412 +123498654854356987405650654985678892327632567215436701 +210567569983543292314321569876301765438761078904589898 +325456478754330101223433478765432654349652987615678765 \ No newline at end of file diff --git a/day_10/solve_1.ts b/day_10/solve_1.ts new file mode 100644 index 0000000..40af7a1 --- /dev/null +++ b/day_10/solve_1.ts @@ -0,0 +1,76 @@ +export type HeightMap = number[][]; + +export async function readMap(path: string): Promise { + const text = await Deno.readTextFile(path); + return text.split("\n").map((row) => + row.trim().split("").map((cell) => { + const ret = parseInt(cell); + if (isNaN(ret)) { + return -5; + } + return ret; + }) + ); +} + +export function displayMap(map: HeightMap) { + console.log(map.map((row) => row.map(x=> x < 0 ? "." : x).join("")).join("\n")); +} + +export type Pos = [number, number]; + +export function getCell(map: HeightMap, [x, y]: Pos): number { + return map[y][x]; +} + +export const TOP_HEIGHT = 9; + +export function countReachableTop(pos: Pos, map: HeightMap): number { + const stack: { + pos: Pos; + height: number; + }[] = []; + const visited = new Set(); + stack.push({ pos, height: getCell(map, pos) }); + let count = 0; + while (stack.length > 0) { + const { pos, height } = stack.pop()!; + const [x, y] = pos; + if (getCell(map, pos) === TOP_HEIGHT) { + if (visited.has(pos.toString())) { + continue; + } + visited.add(pos.toString()); + count++; + continue; + } + for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) { + const [nx, ny] = [x + dx, y + dy]; + if (nx < 0 || nx >= map[0].length || ny < 0 || ny >= map.length) { + continue; + } + const nheight = getCell(map, [nx, ny]); + if (nheight == height + 1) { + stack.push({ pos: [nx, ny], height: nheight }); + } + } + } + return count; +} + +if (import.meta.main) { + const map = await readMap("input.txt"); + displayMap(map); + const data = map.map((row, y) => { + return row.map((cell, x) => { + if (cell === 0) { + const count = countReachableTop([x, y], map); + console.log("count reachable top from", [x, y]); + return count; + } + return -5; + }); + }); + displayMap(data); + console.log(data.flat().reduce((a, b) => a + (b < 0 ? 0 : b ), 0)); +} diff --git a/day_10/solve_2.ts b/day_10/solve_2.ts new file mode 100644 index 0000000..20f731d --- /dev/null +++ b/day_10/solve_2.ts @@ -0,0 +1,51 @@ +import { Pos, HeightMap, getCell, TOP_HEIGHT, readMap, displayMap } from "./solve_1.ts"; + +function countReachableTop(pos: Pos, map: HeightMap): number { + const stack: { + pos: Pos; + height: number; + }[] = []; + // const visited = new Set(); + stack.push({ pos, height: getCell(map, pos) }); + let count = 0; + while (stack.length > 0) { + const { pos, height } = stack.pop()!; + const [x, y] = pos; + if (getCell(map, pos) === TOP_HEIGHT) { + // if (visited.has(pos.toString())) { + // continue; + // } + // visited.add(pos.toString()); + count++; + continue; + } + for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) { + const [nx, ny] = [x + dx, y + dy]; + if (nx < 0 || nx >= map[0].length || ny < 0 || ny >= map.length) { + continue; + } + const nheight = getCell(map, [nx, ny]); + if (nheight == height + 1) { + stack.push({ pos: [nx, ny], height: nheight }); + } + } + } + return count; +} + +if (import.meta.main) { + const map = await readMap("input.txt"); + displayMap(map); + const data = map.map((row, y) => { + return row.map((cell, x) => { + if (cell === 0) { + const count = countReachableTop([x, y], map); + console.log("count reachable top from", [x, y]); + return count; + } + return -5; + }); + }); + displayMap(data); + console.log(data.flat().reduce((a, b) => a + (b < 0 ? 0 : b ), 0)); +}