diff --git a/2024/06/runner.ts b/2024/06/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2024/06/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/06/task.ts b/2024/06/task.ts new file mode 100644 index 0000000..ace6831 --- /dev/null +++ b/2024/06/task.ts @@ -0,0 +1,65 @@ +import {ComplexNumber} from '../../base/math' + +export async function taskOne(input: string[]): Promise { + const pos = new Set() + const guardDir = new ComplexNumber(-1,0) + let guard = new ComplexNumber(0,0) + for (let y = 0; y < input.length; y++) { + for (let x = 0; x < input[0].length; x++) { + if (input[y][x] == '^') guard = new ComplexNumber(y,x) + } + } + while(true) { + pos.add(guard.rel + '-' + guard.img) + const newPos = guard.add(guardDir) + if (!(newPos.rel < input.length && newPos.rel >= 0 && newPos.img < input[0].length && newPos.img >= 0)) break + if (input[newPos.rel][newPos.img] == '#') guardDir.mulAdd(ComplexNumber.fromImg(-1)) + else guard = newPos + } + console.log(pos.size) +} + +export async function taskTwo(input: string[]): Promise { + let count = 0 + let guardStart = new ComplexNumber(0,0) + for (let y = 0; y < input.length; y++) { + for (let x = 0; x < input[0].length; x++) { + if (input[y][x] == '^') guardStart = new ComplexNumber(y,x) + } + } + const pos = new Set() + const guardDir = new ComplexNumber(-1,0) + let guard = guardStart.add(new ComplexNumber(0,0)) + while(true) { + pos.add(guard.rel + '-' + guard.img) + const newPos = guard.add(guardDir) + if (!(newPos.rel < input.length && newPos.rel >= 0 && newPos.img < input[0].length && newPos.img >= 0)) break + if (input[newPos.rel][newPos.img] == '#') guardDir.mulAdd(ComplexNumber.fromImg(-1)) + else guard = newPos + } + const tries = Array.from(pos).map(i => i.split('-').map(Number)) + + for (const t of tries) { + const x = t[1] + const y = t[0] + if (input[y][x] == '#') continue + const pos = new Set() + const guardDir = new ComplexNumber(-1,0) + let guard = guardStart.add(new ComplexNumber(0,0)) + while(true) { + const k = guard.rel + '-' + guard.img + '-' + guardDir.rel + '-' + guardDir.img + if (pos.has(k)) { + count++ + break + } + pos.add(k) + const newPos = guard.add(guardDir) + if (!(newPos.rel < input.length && newPos.rel >= 0 && newPos.img < input[0].length && newPos.img >= 0)) { + break + } + if (input[newPos.rel][newPos.img] == '#' || (newPos.rel == y && newPos.img == x)) guardDir.mulAdd(ComplexNumber.fromImg(-1)) + else guard = newPos + } + } + console.log(count) +} \ No newline at end of file diff --git a/2024/README.md b/2024/README.md index a7c4a37..bf816f0 100644 --- a/2024/README.md +++ b/2024/README.md @@ -1,5 +1,5 @@ # 2024 -![](https://img.shields.io/badge/stars%20⭐-10-yellow) +![](https://img.shields.io/badge/stars%20⭐-12-yellow) |Day|Language|Leaderboard (Part 1 / Part 2)| |--|--|--| @@ -7,4 +7,5 @@ |2|TypeScript|3058 / 2617| |3|TypeScript|6107 / 2643| |4|TypeScript|2103 / 1654| -|5|TypeScript|4252 / 4287| \ No newline at end of file +|5|TypeScript|4252 / 4287| +|6|TypeScript|527 / 726| \ No newline at end of file diff --git a/README.md b/README.md index bea9145..a5ac6c1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # My Advent of Code Solutions -![](https://img.shields.io/badge/Total%20stars%20⭐-421-yellow) +![](https://img.shields.io/badge/Total%20stars%20⭐-423-yellow) ## What language do I use? Whatever I feel like today. But it will most likely either be Java, Python, C++, Typescript or Haskell @@ -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⭐-10-yellow) +![](https://img.shields.io/badge/stars%20⭐-12-yellow) |Day|Language| |--|--| @@ -18,6 +18,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++, |3|TypeScript| |4|TypeScript| |5|TypeScript| +|6|TypeScript|

2023