From 14b7ef5cf5b6025c3b1a026aac582588a4e27a6c Mon Sep 17 00:00:00 2001 From: Alex | Kronox Date: Sat, 7 Dec 2024 12:12:41 +0100 Subject: [PATCH] finish 2018 --- 2018/17/task.ts | 182 ++++++++++++++++++++++------------------------ 2018/25/runner.ts | 29 ++++++++ 2018/25/task.ts | 40 ++++++++++ 2018/README.md | 2 +- README.md | 6 +- 5 files changed, 158 insertions(+), 101 deletions(-) create mode 100644 2018/25/runner.ts create mode 100644 2018/25/task.ts diff --git a/2018/17/task.ts b/2018/17/task.ts index 14fbee3..9c3732d 100644 --- a/2018/17/task.ts +++ b/2018/17/task.ts @@ -1,7 +1,26 @@ +export async function taskOne(input: string[]): Promise { + const grid = task(input) + let c = 0 + for (let x = 0; x < grid.length; x++) { + for (let y = 0; y < grid[0].length; y++) { + if (grid[x][y] == WaterState.WATER || grid[x][y] == WaterState.FLOWING) c++ + } + } + console.log(c) +} -import { readdir, writeFile } from 'fs/promises'; +export async function taskTwo(input: string[]): Promise { + const grid = task(input) + let c = 0 + for (let x = 0; x < grid.length; x++) { + for (let y = 0; y < grid[0].length; y++) { + if (grid[x][y] == WaterState.WATER) c++ + } + } + console.log(c) +} -export async function taskOne(input: string[]): Promise { +function task(input: string[]) { const ranges = parseInput(input) let minX = 500 let minY = Infinity @@ -23,109 +42,95 @@ export async function taskOne(input: string[]): Promise { } } } - - const yLen = grid[0].length - grid[500-minX][0] = WaterState.WATER - recurseFrom(500-minX, 0,) - //print(grid) - - await writeFile("test.txt", toStringArray(grid).join('\n')) - - function recurseFrom(x: number, y: number) { - if (grid[x][y+1] == WaterState.NONE) { - let curY = y - while((curY + 1) < yLen && grid[x][curY+1] == WaterState.NONE) curY++ - if ((curY + 1) < yLen) { - let filledRow = true - while(curY > y) { - while(y < curY && canFillRow(x,curY)) { - fillRow(x,curY) - filledRow = true - curY-- + for (let y = grid[0].length-2; y >= 0; y--) { + let x = 0 + let hadLeft = false + let hadFloorSinceLast = true + while(x < grid.length) { + if (grid[x][y] == WaterState.SAND) { + if (hadLeft && hadFloorSinceLast) { + let dX = x - 1 + while(grid[dX][y] != WaterState.SAND) { + grid[dX][y] = WaterState.POTENTIAL_WATER + dX-- } - if (filledRow) - fillDropRow(x, curY) - else - curY-- - filledRow = false } - - } - curY = y + 1 - while(grid[x][curY] == WaterState.NONE) { - grid[x][curY] = WaterState.WATER - curY++ - } - - - + hadFloorSinceLast = true + hadLeft = true + } + if (grid[x][y+1] == WaterState.NONE) hadFloorSinceLast = false + x++ } } + const yLen = grid[0].length + drop(500-minX,0) + return grid - function canFillRow(x: number, y:number) { - let cX = x - while(cX < grid.length && grid[cX][y] != WaterState.SAND && grid[cX][y+1] != WaterState.NONE) cX++ - if (cX >= grid.length || grid[cX][y+1] == WaterState.NONE) return false - cX = x - while(cX >= 0 && grid[cX][y] != WaterState.SAND && grid[cX][y+1] != WaterState.NONE) cX-- - if (cX < 0 || grid[cX][y+1] == WaterState.NONE) return false - return true - } + - function fillRow(x: number, y: number) { - let cX = x - while(cX >= 0 && grid[cX][y] != WaterState.SAND && grid[cX][y+1] != WaterState.NONE) cX-- - - cX++ - while(grid[cX][y] != WaterState.SAND) { - grid[cX][y] = WaterState.WATER - cX++ + function drop(x: number, y: number) { + while(y < yLen && grid[x][y] == WaterState.NONE) { + grid[x][y] = WaterState.FLOWING + y++ } - } + if (y >= yLen) return + if (grid[x][y] == WaterState.WATER || grid[x][y] == WaterState.FLOWING) return - function fillDropRow(x: number, y: number) { - let cX = x - while(cX < grid.length && grid[cX][y+1] != WaterState.NONE && grid[cX][y] != WaterState.SAND) { - grid[cX][y] = WaterState.WATER - cX++ - } - if (cX < grid.length && grid[cX][y+1] == WaterState.NONE && grid[cX][y] != WaterState.SAND) { - grid[cX][y] = WaterState.WATER - recurseFrom(cX,y) + fillPotential(x,y) + + let dX = x + 1 + y-- + while((grid[dX][y+1] == WaterState.SAND || grid[dX][y+1] == WaterState.WATER) && grid[dX][y] == WaterState.NONE) { + grid[dX][y] = WaterState.FLOWING + dX++ } - cX = x - 1 - while(cX >= 0 && grid[cX][y+1] != WaterState.NONE && grid[cX][y] != WaterState.SAND) { - grid[cX][y] = WaterState.WATER - cX-- + + if (grid[dX][y] == WaterState.NONE) { + drop(dX, y) } - if (cX >= 0 && grid[cX][y+1] == WaterState.NONE && grid[cX][y] != WaterState.SAND) { - grid[cX][y] = WaterState.WATER - recurseFrom(cX,y) + dX = x - 1 + while((grid[dX][y+1] == WaterState.SAND || grid[dX][y+1] == WaterState.WATER) && grid[dX][y] == WaterState.NONE) { + grid[dX][y] = WaterState.FLOWING + dX-- + } + if (grid[dX][y] == WaterState.NONE) { + drop(dX, y) } } - let count = 0 - - for (let x = 0; x < grid.length; x++) { - for (let y = 0; y < grid[x].length; y++) { - if (grid[x][y] == WaterState.WATER) count++ + function fillPotential(x: number, y: number) { + let tY = y + while (grid[x][tY] == WaterState.POTENTIAL_WATER) { + let dX = x + while(grid[dX][tY] != WaterState.SAND) { + grid[dX][tY] = WaterState.WATER + if (grid[dX][tY+1] == WaterState.POTENTIAL_WATER) { + fillPotential(dX,tY+1) + } + dX++ + } + dX = x + while(grid[dX][tY] != WaterState.SAND) { + grid[dX][tY] = WaterState.WATER + if (grid[dX][tY+1] == WaterState.POTENTIAL_WATER) { + fillPotential(dX,tY+1) + } + dX-- + } + tY++ } } - console.log(count) } -export async function taskTwo(input: string[]): Promise { - console.log("Unimplemented"); -} interface Range { x1: number, x2: number, y1: number, y2: number } enum WaterState { - NONE, SAND, WATER + NONE, SAND, WATER, POTENTIAL_WATER, FLOWING } function parseInput(input: string[]) { @@ -148,21 +153,4 @@ function parseInput(input: string[]) { } return r }) -} - -function print(grid: WaterState[][]) { - console.log(toStringArray(grid)) -} - -function toStringArray(grid: WaterState[][]) { - const p = Array.from({length: grid[0].length}, () => Array.from({length: grid.length}, () => ' ')) - for (let x = 0; x < grid.length; x++) { - for (let y = 0; y < grid[x].length; y++) { - if (grid[x][y] == WaterState.WATER) p[y][x] = '~' - if (grid[x][y] == WaterState.SAND) p[y][x] = '#' - //if (grid[x][y] == WaterState.TEST1) p[y][x] = '1' - //if (grid[x][y] == WaterState.TEST2) p[y][x] = '2' - } - } - return p.map(i => i.join('')) } \ No newline at end of file diff --git a/2018/25/runner.ts b/2018/25/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2018/25/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/2018/25/task.ts b/2018/25/task.ts new file mode 100644 index 0000000..0a05719 --- /dev/null +++ b/2018/25/task.ts @@ -0,0 +1,40 @@ +export async function taskOne(input: string[]): Promise { + const points = input.map(i=>i.split(',').map(Number)) + const graph: number[][] = Array.from({length:input.length}, () => []) + + for (let i = 0; i < points.length; i++) { + for (let j = i+1; j < points.length; j++) { + let dist = [ + points[i][0]-points[j][0], + points[i][1]-points[j][1], + points[i][2]-points[j][2], + points[i][3]-points[j][3], + ].map(Math.abs).reduce((a,b)=>a+b,0) + if (dist <= 3) { + graph[i].push(j) + graph[j].push(i) + } + } + } + + const visited = new Set + let groups = 0 + for (let i = 0; i < graph.length; i++) { + if (visited.has(i)) continue + groups++ + const stack = [i] + while(stack.length > 0) { + const q = stack.pop()! + if (visited.has(q)) continue + visited.add(q) + for (const n of graph[q]) { + stack.push(n) + } + } + } + console.log(groups) +} + +export async function taskTwo(input: string[]): Promise { + console.log("Unimplemented"); +} \ No newline at end of file diff --git a/2018/README.md b/2018/README.md index fbb66c8..c9119e5 100644 --- a/2018/README.md +++ b/2018/README.md @@ -1,4 +1,4 @@ # 2018 -![](https://img.shields.io/badge/stars%20⭐-46-yellow) +![](https://img.shields.io/badge/stars%20⭐-50-yellow) Puzzles not solved during event. \ No newline at end of file diff --git a/README.md b/README.md index f306a47..b6b53df 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # My Advent of Code Solutions -![](https://img.shields.io/badge/Total%20stars%20⭐-423-yellow) +![](https://img.shields.io/badge/Total%20stars%20⭐-443-yellow) ## What language do I use? Whatever I feel like today. But it will most likely either be Java, Python, C++, Typescript or Haskell @@ -40,11 +40,11 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

2019

-![](https://img.shields.io/badge/stars%20⭐-25-yellow) +![](https://img.shields.io/badge/stars%20⭐-29-yellow)

2019

-![](https://img.shields.io/badge/stars%20⭐-46-yellow) +![](https://img.shields.io/badge/stars%20⭐-50-yellow)

2017