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,10 +8,9 @@ class DayOne implements Solvable {
public part1(): DayOne {
const vals = this.input
.trim()
.split("\n")
.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()
const right = vals.map(line => line[1]).sort()
const diff: number[] = []
@ -25,17 +24,17 @@ class DayOne implements Solvable {
}
public part2(): DayOne {
const vals = this.input.trim().split("\n").map(line => line.split(' '))
const left = vals.map(line => Number.parseInt(line[0])).sort()
const right = vals.map(line => Number.parseInt(line[1])).sort()
const vals = this.input.trim().split("\n").map(line => line.split(' '))
const left = vals.map(line => Number.parseInt(line[0])).sort()
const right = vals.map(line => Number.parseInt(line[1])).sort()
const countedOccurences: Map<number, number> = new Map()
left.forEach(left_val => countedOccurences.set(left_val, right.filter(right_val => left_val === right_val).length))
const result = countedOccurences
.entries()
.map(([left_val, occurences]) => left_val * occurences)
.reduce((prev, curr) => prev + curr)
console.log(result)
const countedOccurences: Map<number, number> = new Map()
left.forEach(left_val => countedOccurences.set(left_val, right.filter(right_val => left_val === right_val).length))
const result = countedOccurences
.entries()
.map(([left_val, occurences]) => left_val * occurences)
.reduce((prev, curr) => prev + curr)
console.log(result)
return this
}
}

View file

@ -8,15 +8,15 @@ class DayFour implements 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
.map(line => line.join('')
.matchAll(/(XMAS|SAMX)/g)
.map(match => match[0])
.toArray().length)
.matchAll(/(XMAS|SAMX)/g)
.map(match => match[0])
.toArray().length)
.reduce((prev, curr) => prev + curr)
// b/y = height
@ -24,11 +24,11 @@ class DayFour implements Solvable {
// start top right
const getDiagonals = (mat: string[][]) => {
let [b,a] = [0, 140 - 1]
let [b, a] = [0, 140 - 1]
const lines = []
while (b < mat.length) {
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]
lines.push(line.split(''))
@ -44,7 +44,7 @@ class DayFour implements Solvable {
// 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 downLeft = countOccHorizontal(getDiagonals(matrix.reverse()))

View file

@ -5,10 +5,11 @@ import type { Solvable } from "../solvable.ts";
import { readInput } from "../utils.ts";
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)))
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[][] = []
followsRules = (update: number[]): [boolean, number, number] => {
@ -32,6 +33,7 @@ class DayFive implements Solvable {
if (this.followsRules(update)[0]) {
validUpdates.push(update)
} else {
// store these for part 2
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 {
return Deno.readTextFileSync('./inputs/' + day + '.txt')
return Deno.readTextFileSync('./inputs/' + day + '.txt').trim()
}
export { readInput };