looks about right

This commit is contained in:
mtrx 2024-12-07 23:07:55 +01:00
parent 3abf3f3bce
commit 3f33f23c96
4 changed files with 32 additions and 18 deletions

View file

@ -7,13 +7,13 @@ const solvables: Solvable[] = [];
// need to wrap this stuff in a function to not trip the top level await
(async () => {
// cool dynamic module loading
for (const solution of solutionFiles){
for (const solution of solutionFiles) {
await import('./solutions/' + solution.name)
}
if (import.meta.main) {
// only solve the latest day
solvables.toReversed()[0].part1().part2()
solvables.toReversed()[0].part1().then(solvable => solvable.part2())
}
})()

View file

@ -2,7 +2,7 @@
import { solvables } from "../main.ts";
import type { Solvable } from "../solvable.ts";
import { readInput } from "../utils.ts";
import { readInput, delay } from "../utils.ts";
type Direction = "up" | "right" | "down" | "left"
@ -43,10 +43,10 @@ class DaySix implements Solvable {
// 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[0].length,
up: y >= 1,
down: y <= this.map.length,
left: x >= 1,
right: x <= this.map[0].length,
}
// if oob, return true
@ -54,7 +54,7 @@ class DaySix implements Solvable {
console.log('went oob')
return [true, x, y, direction]
} else {
console.log(y, x, 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] === '#') {
@ -67,23 +67,35 @@ class DaySix implements Solvable {
// 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 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) {
screenMat[y][x] = 'X'
}
const screen: string = screenMat.map((line: string[]) => line.join('')).join('\n')
return screen
}
public part1(): Solvable {
public async part1(): Promise<Solvable> {
// find starting position
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
// let i = 0
while (!outOfBounds) {
[outOfBounds, x, y, direction] = this.walk(x, y, direction)
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)
// }
// i++
}
const path = this.visitedLoc.keys().toArray()
@ -92,12 +104,12 @@ class DaySix implements Solvable {
console.log(path)
console.log(result)
return this
return new Promise(() => this)
}
public part2(): Solvable {
public async part2(): Promise<Solvable> {
console.log()
return this
return new Promise(() => this)
}
}

View file

@ -1,6 +1,6 @@
interface Solvable {
part1(): Solvable;
part2(): Solvable;
part1(): Promise<Solvable>;
part2(): Promise<Solvable>;
}
export type { Solvable }

View file

@ -2,4 +2,6 @@ function readInput(day: string): string {
return Deno.readTextFileSync('./inputs/' + day + '.txt').trim()
}
export { readInput };
const delay = (ms: number) => new Promise(res => setTimeout(res, ms));
export { readInput, delay };