aoc24/solutions/06.ts

121 lines
3.9 KiB
TypeScript
Raw Permalink Normal View History

import { solvables } from "../main.ts";
import type { Solvable } from "../solvable.ts";
2024-12-07 23:07:55 +01:00
import { readInput, delay } from "../utils.ts";
2024-12-07 21:40:17 +01:00
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<string, null> = new Map()
// assigned in part1
startingPosition: [number, number] = [0, 0]
2024-12-07 21:40:17 +01:00
turnRight = (direction: Direction): Direction => {
switch (direction) {
case 'up':
2024-12-07 21:40:17 +01:00
return 'right'
case 'right':
return 'down'
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':
return [x, y - 1]
2024-12-07 21:40:17 +01:00
case 'down':
return [x, y + 1]
case 'right':
return [x + 1, y]
case 'left':
2024-12-07 21:40:17 +01:00
return [x - 1, y]
}
}
2024-12-07 21:40:17 +01:00
// returns true if out of bounds
walk = (x: number, y: number, direction: Direction): [boolean, number, number, Direction] => {
const nextStepInBounds = {
2024-12-08 00:30:28 +01:00
up: y > 0,
down: y < this.map.length - 1,
left: x > 0,
right: x < this.map[0].length - 1,
}
2024-12-07 21:40:17 +01:00
// if oob, return true
if (!nextStepInBounds[direction]) {
return [true, x, y, direction]
} else {
2024-12-07 21:40:17 +01:00
// 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 21:40:17 +01:00
// path [y, x][]
visualizePath = (path: string[]): string => {
2024-12-07 23:07:55 +01:00
const width = this.map[0].length
const screenMat: string[][] = []
for (let i = 0; i < width; i++) screenMat.push(this.map[i].slice())
for (const loc of path) {
const [y, x]: number[] = loc.split(',').map(coord => Number.parseInt(coord))
2024-12-07 23:07:55 +01:00
screenMat[y][x] = 'X'
}
2024-12-07 21:40:17 +01:00
const screen: string = screenMat.map((line: string[]) => line.join('')).join('\n')
return screen
}
2024-12-07 23:07:55 +01:00
public async part1(): Promise<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-08 00:30:28 +01:00
let [y, x] = [this.startingPosition[0], this.startingPosition[1]]
let direction: Direction = 'up'
let outOfBounds = false
2024-12-08 00:30:28 +01:00
this.visitedLoc.set(y + ',' + x, null) // starting position is a location
2024-12-07 23:07:55 +01:00
// let i = 0
while (!outOfBounds) {
[outOfBounds, x, y, direction] = this.walk(x, y, direction)
this.visitedLoc.set(y + ',' + x, null)
2024-12-07 23:07:55 +01:00
// console animation thingy
// if (i % 10 === 0) {
// const screen = this.visualizePath(this.visitedLoc.keys().toArray())
// console.log(screen)
// await delay(200)
2024-12-07 23:07:55 +01:00
// }
// i++
}
2024-12-07 21:40:17 +01:00
const path = this.visitedLoc.keys().toArray()
const result: number = path.length
console.log(this.visualizePath(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')
2024-12-07 21:40:17 +01:00
console.log(result)
2024-12-08 00:30:28 +01:00
2024-12-07 23:07:55 +01:00
return new Promise(() => this)
}
2024-12-07 23:07:55 +01:00
public async part2(): Promise<Solvable> {
console.log()
2024-12-07 23:07:55 +01:00
return new Promise(() => this)
}
}
solvables.push(new DaySix())