2024 day 10

This commit is contained in:
2024-12-10 16:27:50 +01:00
parent 3462fbf3bd
commit 442703f847
4 changed files with 229 additions and 0 deletions

40
2024/day-10/day10.test.ts Normal file
View File

@@ -0,0 +1,40 @@
import { expect, test } from "bun:test";
import * as path from "node:path";
import { part1, part2 } from "./day10.ts";
test("day 10, part 1", async () => {
const testInput = await Bun.file(
path.resolve(__dirname, "test-input.txt"),
).text();
const input = await Bun.file(path.resolve(__dirname, "input.txt")).text();
console.log("\n\n");
// const testResult = part1(testInput);
// console.log("Test data:", testResult);
// expect(testResult).toEqual(36);
const finalResult = part1(input);
console.log("Full data:", finalResult);
expect(finalResult).toEqual(682);
console.log("\n\n");
});
test("day 10, part 2", async () => {
const testInput = await Bun.file(
path.resolve(__dirname, "test-input.txt"),
).text();
const input = await Bun.file(path.resolve(__dirname, "input.txt")).text();
const testResult = part2(testInput);
console.log("\n\n");
console.log("Test data:", testResult);
expect(testResult).toEqual(81);
const finalResult = part2(input);
console.log("Full data:", finalResult);
expect(finalResult).toEqual(1511);
console.log("\n\n");
});

127
2024/day-10/day10.ts Normal file
View File

@@ -0,0 +1,127 @@
export function part1(input: string) {
let final = 0;
const trailheads = [];
const lines = input.split("\n");
const grid = lines.map((l) => l.split("").map(Number));
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const heights = line.split("").map(Number);
for (let j = 0; j < heights.length; j++) {
const height = heights[j];
if (height === 0) {
trailheads.push([i, j]);
}
}
}
for (const th of trailheads) {
const [x, y] = th;
const finalPointsReached = [];
function foo(path: [number, number][]) {
if (path.length === 10) {
finalPointsReached.push(path.at(-1));
return;
}
const current = path.at(-1);
const currentHeight = path.length - 1;
if (!current) {
throw new Error("must have at least one element");
}
const prev = path.at(-2);
const [pX, pY] = prev ?? [];
const [cX, cY] = current;
const up = grid[cX - 1]?.[cY];
const down = grid[cX + 1]?.[cY];
const right = grid[cX]?.[cY + 1];
const left = grid[cX]?.[cY - 1];
const isPrevUp = pX === cX - 1 && pY === cY;
const isPrevDown = pX === cX + 1 && pY === cY;
const isPrevRight = pX === cX && pY === cY + 1;
const isPrevLeft = pX === cX && pY === cY - 1;
if (!isPrevUp && up === currentHeight + 1) {
foo([...path, [cX - 1, cY]]);
}
if (!isPrevDown && down === currentHeight + 1) {
foo([...path, [cX + 1, cY]]);
}
if (!isPrevRight && right === currentHeight + 1) {
foo([...path, [cX, cY + 1]]);
}
if (!isPrevLeft && left === currentHeight + 1) {
foo([...path, [cX, cY - 1]]);
}
}
foo([th]);
const uniqueFinalPoints = [
...new Set(finalPointsReached.map((x) => x.join(","))),
].map((x) => x.split(","));
final += uniqueFinalPoints.length;
}
return final;
}
export function part2(input: string) {
let final = 0;
const trailheads = [];
const lines = input.split("\n");
const grid = lines.map((l) => l.split("").map(Number));
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const heights = line.split("").map(Number);
for (let j = 0; j < heights.length; j++) {
const height = heights[j];
if (height === 0) {
trailheads.push([i, j]);
}
}
}
for (const th of trailheads) {
const [x, y] = th;
function foo(path: [number, number][]) {
if (path.length === 10) {
final++;
return;
}
const current = path.at(-1);
const currentHeight = path.length - 1;
if (!current) {
throw new Error("must have at least one element");
}
const prev = path.at(-2);
const [pX, pY] = prev ?? [];
const [cX, cY] = current;
const up = grid[cX - 1]?.[cY];
const down = grid[cX + 1]?.[cY];
const right = grid[cX]?.[cY + 1];
const left = grid[cX]?.[cY - 1];
const isPrevUp = pX === cX - 1 && pY === cY;
const isPrevDown = pX === cX + 1 && pY === cY;
const isPrevRight = pX === cX && pY === cY + 1;
const isPrevLeft = pX === cX && pY === cY - 1;
if (!isPrevUp && up === currentHeight + 1) {
foo([...path, [cX - 1, cY]]);
}
if (!isPrevDown && down === currentHeight + 1) {
foo([...path, [cX + 1, cY]]);
}
if (!isPrevRight && right === currentHeight + 1) {
foo([...path, [cX, cY + 1]]);
}
if (!isPrevLeft && left === currentHeight + 1) {
foo([...path, [cX, cY - 1]]);
}
}
foo([th]);
}
return final;
}
function logLines(arr: Array<unknown>) {
for (const x of arr) {
console.log(x);
}
}

54
2024/day-10/input.txt Normal file
View File

@@ -0,0 +1,54 @@
034565433432321821235456556798712438943432345432101010
125674344341210930012367349897601123876501656543238923
545981265210523841112398232098532014101665437654147014
436780378019654756501454101123442125012878328956056985
521091489328765661498760130210356916763969012347841076
342698456410644570187321221202197809854054521078932987
434784387569653089298435430143087612870123654169410673
525601295678762120387589347834561543965434783254321034
510567856789854871456678956921670123457650199165890321
654356943889903962345665067830589894398743278056785410
701245012958712653434784167845678765210634565410156789
898434321867012344321093254976104543231023876321943089
686321780154102101287765323987267650122012989423872176
567980691233283237898894015416398532143606788714565765
408974500543098946907623234505458545014545698601159854
312363217632127655416510107612369876105894332589018345
221457898565634562323421458733478983276701201478121230
100656967874567641032102369021098744589856782363210121
789345450923498634543001078112321654678345891054308701
654212321012654521694210981205430903543232654143489610
503403430156787610784300870376567812654121743267876523
012567567343898525695421889487854925780010893210965432
123498788210123436786438976598943056891234984921014101
019567699101898345877567887678762147878765675870123012
108754543210567212968900194589054038969454566767636323
205610121056750303453213293032123229454323879098545438
014981232347841219874984782143210110301012978121654569
123672654338932306705675676154560121212307665430743478
298543789221069455012310145069430430925408578989832387
567010176109178764563498232178521567876519454567601296
432123485458769853074567310123678106898323343218970345
012034394367458102189601489433439256765401252102181234
123465210210343012078732676512508349850106769843098321
054896990101212043896543216109612556743219856784587410
969887889654302158987894107898743445654340145697656531
878791078745983467856765545677654354567854234548545621
745622363215676500345653231987980123498960129639343210
034215454301876211256570120345678154323870038721658343
122100189321945308967981011234569067212721801290569012
543893276430932457898876323233432178008934980387378143
456764345567801966501895400145567889127645673456269654
369632105478765873432689312176876901234560012562158765
278543254309894569546576543089985432321071239873043210
103450569212723278637434010034394321810980548954560127
894321078112010198728922121165223010901289687643073438
765697151003434587017213033278112987874398796542189569
056788769876524326501304544569001056965834321233675678
145679458985015415432987693432102345216945610344569547
230989347034376102341056780540989104307898700123678632
321078234123289211652346791691071234487465410878766781
989165106000105690789905882782360943296578321969215690
076234245612234782349814943485456850123489100154304385
145210238763145671456723876596306765012101256701235276
234300149854078980365432101487217898721032349810120123

View File

@@ -0,0 +1,8 @@
89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732