From 1e8a8a0ca0fa101083451647467d3d19acef9112 Mon Sep 17 00:00:00 2001 From: Alex | Kronox Date: Fri, 20 Dec 2024 06:37:33 +0100 Subject: [PATCH] 2024 day 20 --- 2024/20/runner.ts | 29 ++++++++ 2024/20/task.ts | 164 ++++++++++++++++++++++++++++++++++++++++++++++ 2024/README.md | 5 +- README.md | 4 +- 4 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 2024/20/runner.ts create mode 100644 2024/20/task.ts diff --git a/2024/20/runner.ts b/2024/20/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2024/20/runner.ts @@ -0,0 +1,29 @@ +import { readFile } from "fs/promises"; +import { taskOne, taskTwo } from "./task"; + +async function main() { + let lastArgument = process.argv.pop() as string; + let taskNumber = 1; + let isTest = false; + + if (lastArgument === "test") { + isTest = true; + taskNumber = parseInt(process.argv.pop() as string); + } else { + taskNumber = parseInt(lastArgument) + } + + const fileToLoad = isTest ? "test.in" : "solve.in"; + const fileContents = await readFile(fileToLoad, "utf-8") + + const lines = fileContents.split("\n"); + + if (taskNumber === 1) { + await taskOne(lines); + } + if (taskNumber === 2) { + await taskTwo(lines); + } +} + +void main(); \ No newline at end of file diff --git a/2024/20/task.ts b/2024/20/task.ts new file mode 100644 index 0000000..e347afa --- /dev/null +++ b/2024/20/task.ts @@ -0,0 +1,164 @@ +import { Queue } from '../../base' + +export async function taskOne(input: string[]): Promise { + const grid = input.map(i => i.split('')) + + let start = [0,0] + let end = [0,0] + for (let y = 0; y < grid.length; y++) { + for (let x = 0; x < grid[y].length; x++) { + if (grid[y][x] == 'S') start = [x,y] + if (grid[y][x] == 'E') end = [x,y] + } + } + const queue = new Queue() + const distancesFromStart = new Map() + const distancesFromEnd = new Map() + queue.push({x:start[0], y: start[1], d: 0}) + + while(!queue.isEmpty()) { + const q = queue.pop() + if (grid[q.y][q.x] == '#') continue + const k = q.x + '|' + q.y + if (distancesFromStart.has(k)) continue + distancesFromStart.set(k, q.d) + + for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) { + queue.push({ + x: q.x + dir[0], + y: q.y + dir[1], + d: q.d+1 + }) + } + } + queue.push({x:end[0], y: end[1], d: 0}) + while(!queue.isEmpty()) { + const q = queue.pop() + if (grid[q.y][q.x] == '#') continue + const k = q.x + '|' + q.y + if (distancesFromEnd.has(k)) continue + distancesFromEnd.set(k, q.d) + + for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) { + queue.push({ + x: q.x + dir[0], + y: q.y + dir[1], + d: q.d+1 + }) + } + } + let totalDistance = distancesFromStart.get(end[0]+'|'+end[1])! + let s = 0 + for (let y = 0; y < grid.length; y++) { + for (let x = 0; x < grid[y].length; x++) { + if (grid[y][x] == '#') continue + const distanceToGetHere = distancesFromStart.get(x+'|'+y)! + for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) { + if (grid[y+dir[1]][x+dir[0]] == '#') { + try { + if (grid[y+2*dir[1]][x+2*dir[0]] != '#') { + const stillNeeded = distancesFromEnd.get((x+2*dir[0])+'|'+(y+2*dir[1]))! + let totalTime = distanceToGetHere + 2 + stillNeeded + let savedTime = totalDistance - totalTime + if (savedTime >= 100) { + s++ + } + } + } catch(e){} + + } + } + + } + } + console.log(s) + + + interface State { + x: number + y: number + d: number + } + +} + +export async function taskTwo(input: string[]): Promise { + const grid = input.map(i => i.split('')) + + let start = [0,0] + let end = [0,0] + for (let y = 0; y < grid.length; y++) { + for (let x = 0; x < grid[y].length; x++) { + if (grid[y][x] == 'S') start = [x,y] + if (grid[y][x] == 'E') end = [x,y] + } + } + const queue = new Queue() + const distancesFromStart = new Map() + const distancesFromEnd = new Map() + queue.push({x:start[0], y: start[1], d: 0}) + + while(!queue.isEmpty()) { + const q = queue.pop() + if (grid[q.y][q.x] == '#') continue + const k = q.x + '|' + q.y + if (distancesFromStart.has(k)) continue + distancesFromStart.set(k, q.d) + + for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) { + queue.push({ + x: q.x + dir[0], + y: q.y + dir[1], + d: q.d+1 + }) + } + } + queue.push({x:end[0], y: end[1], d: 0}) + while(!queue.isEmpty()) { + const q = queue.pop() + if (grid[q.y][q.x] == '#') continue + const k = q.x + '|' + q.y + if (distancesFromEnd.has(k)) continue + distancesFromEnd.set(k, q.d) + + for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) { + queue.push({ + x: q.x + dir[0], + y: q.y + dir[1], + d: q.d+1 + }) + } + } + let totalDistance = distancesFromStart.get(end[0]+'|'+end[1])! + let s = 0 + for (let y = 0; y < grid.length; y++) { + for (let x = 0; x < grid[y].length; x++) { + if (grid[y][x] == '#') continue + const distanceToGetHere = distancesFromStart.get(x+'|'+y)! + for (let nX =-20; nX <= 20; nX++) { + for (let nY = -20; nY <= 20; nY++) { + if (Math.abs(nX) + Math.abs(nY) > 20) continue + if (x + nX < 0 || x + nX >= grid[0].length) continue + if (y + nY < 0 || y + nY >= grid.length) continue + if (grid[y+nY][x+nX] == '#') continue + + const stillNeeded = distancesFromEnd.get((x+nX)+'|'+(y+nY))! + let totalTime = distanceToGetHere + Math.abs(nX) + Math.abs(nY) + stillNeeded + let savedTime = totalDistance - totalTime + if (savedTime >= 100) { + s++ + } + } + } + } + } + console.log(s) + + + interface State { + x: number + y: number + d: number + } +} + diff --git a/2024/README.md b/2024/README.md index db6cb06..d41b265 100644 --- a/2024/README.md +++ b/2024/README.md @@ -1,5 +1,5 @@ # 2024 -![](https://img.shields.io/badge/stars%20⭐-38-yellow) +![](https://img.shields.io/badge/stars%20⭐-40-yellow) |Day|Language|Leaderboard (Part 1 / Part 2)| |--|--|--| @@ -21,4 +21,5 @@ |16|Typescript|347 / 1292| |17|Typescript|486 / 8649| |18|Typescript|420 / 1186| -|19|Typescript|585 / 491| \ No newline at end of file +|19|Typescript|585 / 491| +|20|Typescript|933 / 503| \ No newline at end of file diff --git a/README.md b/README.md index efdca57..58abee2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # My Advent of Code Solutions -![](https://img.shields.io/badge/Total%20stars%20⭐-485yellow) +![](https://img.shields.io/badge/Total%20stars%20⭐-487-yellow) ## What language do I use? Whatever I feel like today. But it will most likely either be Java, Python, C++, TypeScript or Haskell - Lets be honest with ourselfs, it will by TypeScript @@ -9,7 +9,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

2024

-![](https://img.shields.io/badge/stars%20⭐-38-yellow) +![](https://img.shields.io/badge/stars%20⭐-40-yellow) |Day|Language| |--|--|