2024-12-07 02:16:43 +01:00
|
|
|
|
|
|
|
import { solvables } from "../main.ts";
|
|
|
|
|
|
|
|
import type { Solvable } from "../solvable.ts";
|
|
|
|
import { readInput } from "../utils.ts";
|
|
|
|
|
2024-12-07 21:40:17 +01:00
|
|
|
type Direction = "up" | "right" | "down" | "left"
|
2024-12-07 02:16:43 +01:00
|
|
|
|
|
|
|
class DaySix implements Solvable {
|
|
|
|
input: string[][] = readInput('06').split('\n').map(col => col.split(''))
|
|
|
|
map = this.input
|
2024-12-07 21:40:17 +01:00
|
|
|
visitedLoc: Map<[number, number], null> = new Map()
|
2024-12-07 02:16:43 +01:00
|
|
|
|
|
|
|
// assigned in part1
|
|
|
|
startingPosition: [number, number] = [0, 0]
|
|
|
|
|
2024-12-07 21:40:17 +01:00
|
|
|
turnRight = (direction: Direction): Direction => {
|
2024-12-07 02:16:43 +01:00
|
|
|
switch (direction) {
|
|
|
|
case 'up':
|
2024-12-07 21:40:17 +01:00
|
|
|
return 'right'
|
|
|
|
case 'right':
|
|
|
|
return 'down'
|
2024-12-07 02:16:43 +01:00
|
|
|
case 'down':
|
2024-12-07 21:40:17 +01:00
|
|
|
return 'left'
|
|
|
|
case 'left':
|
|
|
|
return 'up'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lookAhead = (x: number, y: number, direction: Direction): [number, number] => {
|
|
|
|
switch (direction) {
|
|
|
|
case 'up':
|
2024-12-07 02:16:43 +01:00
|
|
|
return [x, y - 1]
|
2024-12-07 21:40:17 +01:00
|
|
|
case 'down':
|
|
|
|
return [x, y + 1]
|
2024-12-07 02:16:43 +01:00
|
|
|
case 'right':
|
|
|
|
return [x + 1, y]
|
|
|
|
case 'left':
|
2024-12-07 21:40:17 +01:00
|
|
|
return [x - 1, y]
|
2024-12-07 02:16:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-07 21:40:17 +01:00
|
|
|
// returns true if out of bounds
|
2024-12-07 02:16:43 +01:00
|
|
|
walk = (x: number, y: number, direction: Direction): [boolean, number, number, Direction] => {
|
|
|
|
const nextStepInBounds = {
|
|
|
|
up: y > 1,
|
|
|
|
down: y < this.map.length,
|
|
|
|
left: x > 1,
|
2024-12-07 21:40:17 +01:00
|
|
|
right: x < this.map[0].length,
|
2024-12-07 02:16:43 +01:00
|
|
|
}
|
|
|
|
|
2024-12-07 21:40:17 +01:00
|
|
|
// if oob, return true
|
2024-12-07 02:16:43 +01:00
|
|
|
if (!nextStepInBounds[direction]) {
|
2024-12-07 21:40:17 +01:00
|
|
|
console.log('went oob')
|
2024-12-07 02:16:43 +01:00
|
|
|
return [true, x, y, direction]
|
|
|
|
} else {
|
2024-12-07 21:40:17 +01:00
|
|
|
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]
|
2024-12-07 02:16:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-07 21:40:17 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-12-07 02:16:43 +01:00
|
|
|
public part1(): Solvable {
|
|
|
|
// find starting position
|
2024-12-07 21:40:17 +01:00
|
|
|
this.input.forEach((line, y) => line.forEach((char, x) => { if (char === '^') this.startingPosition = [y, x] }))
|
2024-12-07 02:16:43 +01:00
|
|
|
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)
|
2024-12-07 21:40:17 +01:00
|
|
|
this.visitedLoc.set([y, x], null)
|
2024-12-07 02:16:43 +01:00
|
|
|
}
|
|
|
|
|
2024-12-07 21:40:17 +01:00
|
|
|
const path = this.visitedLoc.keys().toArray()
|
|
|
|
const result: number = path.length
|
|
|
|
console.log(this.visualizePath(path))
|
|
|
|
console.log(path)
|
|
|
|
console.log(result)
|
2024-12-07 02:16:43 +01:00
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
public part2(): Solvable {
|
|
|
|
console.log()
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
solvables.push(new DaySix())
|