From 3f33f23c96edc2ef277096b827b3c1d8fdc54bac Mon Sep 17 00:00:00 2001 From: mtrx Date: Sat, 7 Dec 2024 23:07:55 +0100 Subject: [PATCH] looks about right --- main.ts | 4 ++-- solutions/06.ts | 38 +++++++++++++++++++++++++------------- solvable.ts | 4 ++-- utils.ts | 4 +++- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/main.ts b/main.ts index ceba3fe..adc4ad9 100644 --- a/main.ts +++ b/main.ts @@ -7,13 +7,13 @@ const solvables: Solvable[] = []; // need to wrap this stuff in a function to not trip the top level await (async () => { // cool dynamic module loading - for (const solution of solutionFiles){ + for (const solution of solutionFiles) { await import('./solutions/' + solution.name) } if (import.meta.main) { // only solve the latest day - solvables.toReversed()[0].part1().part2() + solvables.toReversed()[0].part1().then(solvable => solvable.part2()) } })() diff --git a/solutions/06.ts b/solutions/06.ts index df8a735..62b0ee1 100644 --- a/solutions/06.ts +++ b/solutions/06.ts @@ -2,7 +2,7 @@ import { solvables } from "../main.ts"; import type { Solvable } from "../solvable.ts"; -import { readInput } from "../utils.ts"; +import { readInput, delay } from "../utils.ts"; type Direction = "up" | "right" | "down" | "left" @@ -43,10 +43,10 @@ class DaySix implements Solvable { // returns true if out of bounds walk = (x: number, y: number, direction: Direction): [boolean, number, number, Direction] => { const nextStepInBounds = { - up: y > 1, - down: y < this.map.length, - left: x > 1, - right: x < this.map[0].length, + up: y >= 1, + down: y <= this.map.length, + left: x >= 1, + right: x <= this.map[0].length, } // if oob, return true @@ -54,7 +54,7 @@ class DaySix implements Solvable { console.log('went oob') return [true, x, y, direction] } else { - console.log(y, x, direction) + // console.log(y, x, direction) // if obstacle ahead, return false and current location const [nextX, nextY] = this.lookAhead(x, y, direction) if (this.map[nextY][nextX] === '#') { @@ -67,23 +67,35 @@ class DaySix implements Solvable { // path [y, x][] visualizePath = (path: [number, number][]): string => { - const [height, width] = [this.map.length, this.map[0].length] - const screenMat: string[][] = new Array(width).fill(new Array(height).fill('.')) - for (const [y, x] of path) screenMat[y][x] = 'X' + const width = this.map[0].length + const screenMat: string[][] = [] + for (let i = 0; i < width; i++) screenMat.push(this.map[i].slice()) + for (const [y, x] of path) { + screenMat[y][x] = 'X' + } const screen: string = screenMat.map((line: string[]) => line.join('')).join('\n') return screen } - public part1(): Solvable { + public async part1(): Promise { // find starting position this.input.forEach((line, y) => line.forEach((char, x) => { if (char === '^') this.startingPosition = [y, x] })) let [x, y] = [this.startingPosition[0], this.startingPosition[1]] let direction: Direction = 'up' let outOfBounds = false + // let i = 0 while (!outOfBounds) { [outOfBounds, x, y, direction] = this.walk(x, y, direction) this.visitedLoc.set([y, x], null) + // console animation thingy + + // if (i % 10 === 0) { + // const screen = this.visualizePath(this.visitedLoc.keys().toArray()) + // console.log(screen) + // await delay(100) + // } + // i++ } const path = this.visitedLoc.keys().toArray() @@ -92,12 +104,12 @@ class DaySix implements Solvable { console.log(path) console.log(result) - return this + return new Promise(() => this) } - public part2(): Solvable { + public async part2(): Promise { console.log() - return this + return new Promise(() => this) } } diff --git a/solvable.ts b/solvable.ts index 88cd5cb..70ccc76 100644 --- a/solvable.ts +++ b/solvable.ts @@ -1,6 +1,6 @@ interface Solvable { - part1(): Solvable; - part2(): Solvable; + part1(): Promise; + part2(): Promise; } export type { Solvable } \ No newline at end of file diff --git a/utils.ts b/utils.ts index d02d368..e0ee811 100644 --- a/utils.ts +++ b/utils.ts @@ -2,4 +2,6 @@ function readInput(day: string): string { return Deno.readTextFileSync('./inputs/' + day + '.txt').trim() } -export { readInput }; +const delay = (ms: number) => new Promise(res => setTimeout(res, ms)); + +export { readInput, delay };