aoc24/solutions/06.ts

71 lines
1.9 KiB
TypeScript
Raw Normal View History

import { solvables } from "../main.ts";
import type { Solvable } from "../solvable.ts";
import { readInput } from "../utils.ts";
type Direction = "up" | "down" | "left" | "right"
class DaySix implements Solvable {
input: string[][] = readInput('06').split('\n').map(col => col.split(''))
map = this.input
visitedLoc = new Map()
// assigned in part1
startingPosition: [number, number] = [0, 0]
takeStep = (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]
}
}
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,
}
if (!nextStepInBounds[direction]) {
return [true, x, y, direction]
} else {
// TODO check for obstacles
const [newX, newY] = this.takeStep(x, y, direction)
return [false, newX, newY, direction]
}
}
public part1(): Solvable {
// find starting position
this.input.forEach((line, y) => line.forEach((col, x) => { if (col === '^') 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)
}
console.log()
return this
}
public part2(): Solvable {
console.log()
return this
}
}
solvables.push(new DaySix())