From a4d4dc2b23af09ed303a4cdc3f06adc32e45b754 Mon Sep 17 00:00:00 2001 From: mtrx Date: Sun, 8 Dec 2024 00:04:07 +0100 Subject: [PATCH] i really don't know what's going wrong here --- solutions/06.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/solutions/06.ts b/solutions/06.ts index 62b0ee1..9945567 100644 --- a/solutions/06.ts +++ b/solutions/06.ts @@ -9,7 +9,7 @@ type Direction = "up" | "right" | "down" | "left" class DaySix implements Solvable { input: string[][] = readInput('06').split('\n').map(col => col.split('')) map = this.input - visitedLoc: Map<[number, number], null> = new Map() + visitedLoc: Map = new Map() // assigned in part1 startingPosition: [number, number] = [0, 0] @@ -44,9 +44,9 @@ class DaySix implements Solvable { walk = (x: number, y: number, direction: Direction): [boolean, number, number, Direction] => { const nextStepInBounds = { up: y >= 1, - down: y <= this.map.length, + down: y < this.map.length, left: x >= 1, - right: x <= this.map[0].length, + right: x < this.map[0].length, } // if oob, return true @@ -66,17 +66,19 @@ class DaySix implements Solvable { } // path [y, x][] - visualizePath = (path: [number, number][]): string => { + visualizePath = (path: string[]): string => { 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) { + for (const loc of path) { + const [y, x]: number[] = loc.split(',').map(coord => Number.parseInt(coord)) screenMat[y][x] = 'X' } const screen: string = screenMat.map((line: string[]) => line.join('')).join('\n') return screen } + public async part1(): Promise { // find starting position this.input.forEach((line, y) => line.forEach((char, x) => { if (char === '^') this.startingPosition = [y, x] })) @@ -87,13 +89,13 @@ class DaySix implements Solvable { // let i = 0 while (!outOfBounds) { [outOfBounds, x, y, direction] = this.walk(x, y, direction) - this.visitedLoc.set([y, x], null) + 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) + // await delay(200) // } // i++ } @@ -101,7 +103,10 @@ class DaySix implements Solvable { const path = this.visitedLoc.keys().toArray() const result: number = path.length console.log(this.visualizePath(path)) - console.log(path) + + const allDistinct = path.filter((loc: string) => path.indexOf(loc) !== path.lastIndexOf(loc)).length === 0 + console.log(allDistinct ? 'path values distinct' : 'path values not distinct') + console.log(result) return new Promise(() => this)