comments and starting part 6, remove redundant trims

This commit is contained in:
mtrx 2024-12-07 02:16:43 +01:00
parent 4462c12f77
commit 7211835f76
6 changed files with 227 additions and 25 deletions

130
inputs/06.txt Normal file
View file

@ -0,0 +1,130 @@
....................................#............##........#..#.#..#.........#.............#..............#.......................
..................##.........#....................##.....#.......#.......................................#..........#.............
.......#...........#.........................................................................................#.#.#................
.#..........#...............................#....................................#...#.....#...#.#....................#....#......
..........#..........................................#............#.........................#..#......#....#..#..........#........
..........................................................................#..........................................#.........#..
.....#....#...........................#......#..........#........................#..........#.............#...........#..........#
................#.......................#...........#.............................#.....#..#..................................#...
........................................................................#..#.......................................#..............
........#......................#.....#.......#.............#....#.........#....#........................#.............#...........
.............#....#.......#........#..#.......##................#.......................................#.........................
......#..............#.#...#...............#.........................#.....#............#..#.........#...............#............
..................#...#..............................................#................#.......#................#..................
....#...#.............#........................#....................................#......#................#......#..............
..........................................................................................#...#.........##........................
.............#.#...............#.....#.#......#.............................#.......................................#.....#.......
.................................................#................................................................#...............
........#.#......................#......#........#.......................................................#........................
....#........#......#..................................................................................#............#.............
...........#.........#.......#...............#........................#........................................................#.#
........................##..................#........................#..........................................#.............#...
..........#........#........#.........#..................#.....#..............................#..#..............#.................
#..............................................................#..#............................#..........#.......................
.................................................................#...............#...#.#.........#.#.................#...........#
.....................................#.....................#.....................#........#....#....#.............................
#..#................#.....#....................................##............#...............#...#..........#.....................
.................#.....#.....................#................#...........#...................................................#...
..................................#........#.................#....#..................#.......................................#....
..#....#............................#................................................##.........................................#.
...............#......................................................#......##...................................................
.........................#..................................#................##.#..#.#.................#.......#..................
.................#......................................#...#.....................................................................
.........#..........................................................................................#.......#.....................
....##..#.........#..............#..#............#............#...................................#...............................
......#.......................................#................................................................#..................
....#..................#............................#................................................................#............
...............................##.#..............#....#............#.........................................#.....#..............
........#........#....#.................................................................#.....#...................#...............
#...........#..#.........................................#..................................................#..##.......#.....#..#
................#......#...........##..........................................................#....#.............................
..........#..........#.............................................................#..............#.................#..........#..
#.................#....................................................................................#..#..............#........
.....#..............#.........................#.....#...........#......................#..........................................
.....##.....................#...............................................................#.....................................
...................#.................................................................................#.....................#..#...
...............................................^......................#...........................#...............................
.......#...............#.........#.........#......#.......................................................................#.......
........#.........#..#..##.................................#.....#..............#...........#..#........#.........................
..............................................................................#......#.#...............#..#..#.....#....#.........
............#...#.......##............#....#...........................................................................#..........
..................................##........#.....##.......#......................................................................
.......................#...........................................#..................................#...#.........#.............
............#......................................................#....#..........#...................#........................#.
....................................................#......................#..........................#....#......................
..................#.##....................................#..................................#.................#....#...#.........
........#.....................................................................................#...#...............................
....................................................#..........................................#........#..#.#.................#..
....#...........#..#......#.........................#.....................#............................#.....#.#..............#...
.............................#.......#..........#..................................................................#....#.....#...
.....#.#................#...................#...............................................#.....................................
....#........................................#......#.............................#...............................................
.......#..........#........................#.....................................#..............#......#...##.....................
.....#..#.............................##........................................................#.....#..............#.#..........
...........................................#......#......................................#..............#.........................
...................#.........#....#................#.....#.........#....................#............#..#........#...............#
.....#.....................#......................................................................#...............................
...............#.......................................#...#.................#..............#.....................................
.........................#........................................................#...............................................
..................................#..........................................#...........#.....................#..................
.......................#.#............#.........................................#.................................................
..........................#...................#......................#......................................#.....................
.........#.........................#..............................................................#...............................
..#..............................#...............#..................................###..........................................#
.............................................#...............#.....#........................................................#..#..
................#..........................#.......................................#........................................##.#..
......##........#.....................................#............#..................#...............................#...........
..#..................#......................................#....#.......................#................................#.......
........................#...............#....#...#..........................................................................#.....
.............#.#..........#...#....#.................##...........................................................................
......#....#.......................................................................#.....................................#..#.....
.............#.......#............#...#..........#...............#..................................................#..........#..
..........................#...........................................................#.............#.............................
#......................................#........#......#..........................................................................
................#..........................................................#......................................................
..................#................................................................#..........................#..........#.......#
.......#.....#............##..............................................................#....................#.....#......#.....
...........................................#..........#.#............................................#............................
.......#.......................#..........................................................#.......................................
.................#........................#.........#...#............................................................#............
................................................#...................#.........#...........#...........................#...........
..#..............#......#..............................##............#..................#.......#.................................
.#.#.........................................#......##.....##........#.............#..#..................##....#.#...#............
..#.....................#.......#....#...............#...............#...................................................#........
.#...#...............#..................................................................#..........#....#.................#.#.....
.#...................#...................................................................#.............#.......................#..
.....................................#.....#..#...........#...........#..........#....................................#...........
.................................................................................#..#.............##....#...#............#........
......................#....#........#......#...................#.............................................#....................
.......................................#..........................#............................#.#..........................#.....
.#........................##....................................#.......#...........................#..#.......................#..
.........#.........#................................................................#.................#........#..................
..............#.......#.....#....................#........##.............#....#...............#.............#...........#...#.....
.................................................................#..#........#...#...#.........#..................................
................#......##..........#..........#.........#....#............................................#.#.............#.......
...#...................................................................#.......................................#..................
.......#..................#............#.....#................#..#....##.......#...............#..................................
............#............##....#...................#..........................#......................................#...#...#....
......#.....#..........##...............#......#...............................#......#...........................................
.................#.......#.......................................................#............#........#...#.......#...........#..
......................##........#...................#.....#....................#...#....#........................#................
.................#.....#.......#...........#.......#....................##....................................................#.#.
......#....................................#.........................#..................................#................#.#..#...
..............#...........................................................................................................#.......
#.#......#.................................................................................#...........#...#......................
...........................................#.#........#....#............#.....................#...................................
...#.............#.....................................................................................................#..........
..............#..........#............#...#..............#........................................##........#.....#.......#.......
..................##........#.............................................#.........................................###...........
..........#......................#............................................................................#....#..............
.............#........#.............#.#...............#.....#..........#.....#....#...#...........#....#..........................
........#...#.......#........................#.............#..........#........#.......................#..........................
..................................................................#...............#......#...#.......#............................
.............#..........#...................##.............#.........#..................#.........................................
.#.....#..#...#...............#.#.............#...........#.......................................................................
..............#..#.........................#..............#..#..............#.......................#.#...........................
............#.......................#....#................#..........#.......................#.....................#..............
#.................#............................#...##....#.....#.....................................................#............
.............................#........................#..................................................#....#...................
.##...........................................#.....#....#....#.......................#...#.....#...#............#................
........#.............#.........#..............#........................#.....................#...#...............................

View file

@ -8,7 +8,6 @@ class DayOne implements Solvable {
public part1(): DayOne { public part1(): DayOne {
const vals = this.input const vals = this.input
.trim()
.split("\n") .split("\n")
.map(line => line.split(' ').map(col => Number.parseInt(col))) .map(line => line.split(' ').map(col => Number.parseInt(col)))
const left = vals.map(line => line[0]).sort() const left = vals.map(line => line[0]).sort()

View file

@ -8,9 +8,9 @@ class DayFour implements Solvable {
public part1(): Solvable { public part1(): Solvable {
const transpose = (m: string[][]) => m[0].map((_,i) => m.map(x => x[i])) const transpose = (m: string[][]) => m[0].map((_, i) => m.map(x => x[i]))
const matrix = this.input.trim().split('\n').map(line => line.split('')) const matrix = this.input.split('\n').map(line => line.split(''))
const countOccHorizontal = (mat: string[][]) => mat const countOccHorizontal = (mat: string[][]) => mat
.map(line => line.join('') .map(line => line.join('')
@ -24,11 +24,11 @@ class DayFour implements Solvable {
// start top right // start top right
const getDiagonals = (mat: string[][]) => { const getDiagonals = (mat: string[][]) => {
let [b,a] = [0, 140 - 1] let [b, a] = [0, 140 - 1]
const lines = [] const lines = []
while (b < mat.length) { while (b < mat.length) {
let line = '' let line = ''
for (let [y,x] = [b,a]; x < 140 && y < 140; y++,x++) for (let [y, x] = [b, a]; x < 140 && y < 140; y++, x++)
line += mat[y][x] line += mat[y][x]
lines.push(line.split('')) lines.push(line.split(''))
@ -44,7 +44,7 @@ class DayFour implements Solvable {
// console.log(getDiagonals(matrix.reverse())) // console.log(getDiagonals(matrix.reverse()))
console.log(transpose(matrix).map(line=> line.join(''))) console.log(transpose(matrix).map(line => line.join('')))
const downRight = countOccHorizontal(getDiagonals(matrix)) const downRight = countOccHorizontal(getDiagonals(matrix))
const downLeft = countOccHorizontal(getDiagonals(matrix.reverse())) const downLeft = countOccHorizontal(getDiagonals(matrix.reverse()))

View file

@ -5,10 +5,11 @@ import type { Solvable } from "../solvable.ts";
import { readInput } from "../utils.ts"; import { readInput } from "../utils.ts";
class DayFive implements Solvable { class DayFive implements Solvable {
input: string[] = readInput('05').trim().split('\n\n') input: string[] = readInput('05').split('\n\n')
rules: number[][] = this.input[0].split('\n').map((input: string) => input.split('|').map(rule => Number.parseInt(rule))) rules: number[][] = this.input[0].split('\n').map((input: string) => input.split('|').map(rule => Number.parseInt(rule)))
updates: number[][] = this.input[1].split('\n').map(line => line.split(',').map(update => Number.parseInt(update))) updates: number[][] = this.input[1].split('\n').map(line => line.split(',').map(update => Number.parseInt(update)))
// generated in part 1 for part 2
invalidUpdates: number[][] = [] invalidUpdates: number[][] = []
followsRules = (update: number[]): [boolean, number, number] => { followsRules = (update: number[]): [boolean, number, number] => {
@ -32,6 +33,7 @@ class DayFive implements Solvable {
if (this.followsRules(update)[0]) { if (this.followsRules(update)[0]) {
validUpdates.push(update) validUpdates.push(update)
} else { } else {
// store these for part 2
this.invalidUpdates.push(update) this.invalidUpdates.push(update)
} }
}) })

71
solutions/06.ts Normal file
View file

@ -0,0 +1,71 @@
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())

View file

@ -1,5 +1,5 @@
function readInput(day: string): string { function readInput(day: string): string {
return Deno.readTextFileSync('./inputs/' + day + '.txt') return Deno.readTextFileSync('./inputs/' + day + '.txt').trim()
} }
export { readInput }; export { readInput };