diff --git a/solutions/06.ts b/solutions/06.ts index ac9e31d..df8a735 100644 --- a/solutions/06.ts +++ b/solutions/06.ts @@ -4,60 +4,93 @@ import { solvables } from "../main.ts"; import type { Solvable } from "../solvable.ts"; import { readInput } from "../utils.ts"; -type Direction = "up" | "down" | "left" | "right" +type Direction = "up" | "right" | "down" | "left" class DaySix implements Solvable { input: string[][] = readInput('06').split('\n').map(col => col.split('')) map = this.input - visitedLoc = new Map() + visitedLoc: Map<[number, number], null> = new Map() // assigned in part1 startingPosition: [number, number] = [0, 0] - takeStep = (x: number, y: number, direction: Direction): [number, number] => { + turnRight = (direction: Direction): Direction => { switch (direction) { case 'up': - return [x, y + 1] - case 'down': - return [x, y - 1] + return 'right' case 'right': - return [x + 1, y] + return 'down' + case 'down': + return 'left' case 'left': - return [x + 1, y] + return 'up' } } + lookAhead = (x: number, y: number, direction: Direction): [number, number] => { + switch (direction) { + case 'up': + return [x, y - 1] + case 'down': + return [x, y + 1] + case 'right': + return [x + 1, y] + case 'left': + return [x - 1, y] + } + } + + // 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.length, + right: x < this.map[0].length, } + // if oob, return true if (!nextStepInBounds[direction]) { + console.log('went oob') return [true, x, y, direction] } else { - // TODO check for obstacles - const [newX, newY] = this.takeStep(x, y, direction) - return [false, newX, newY, 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] === '#') { + return [false, x, y, this.turnRight(direction)] + } + // if no obstacle ahead, return false and new location + return [false, nextX, nextY, direction] } } + // 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 screen: string = screenMat.map((line: string[]) => line.join('')).join('\n') + return screen + } + public part1(): Solvable { // find starting position - this.input.forEach((line, y) => line.forEach((col, x) => { if (col === '^') this.startingPosition = [y, x] })) + 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 while (!outOfBounds) { [outOfBounds, x, y, direction] = this.walk(x, y, direction) + this.visitedLoc.set([y, x], null) } - - - console.log() + const path = this.visitedLoc.keys().toArray() + const result: number = path.length + console.log(this.visualizePath(path)) + console.log(path) + console.log(result) return this }