diff --git a/inputs/06.txt b/inputs/06.txt new file mode 100644 index 0000000..6849724 --- /dev/null +++ b/inputs/06.txt @@ -0,0 +1,130 @@ +....................................#............##........#..#.#..#.........#.............#..............#....................... +..................##.........#....................##.....#.......#.......................................#..........#............. +.......#...........#.........................................................................................#.#.#................ +.#..........#...............................#....................................#...#.....#...#.#....................#....#...... +..........#..........................................#............#.........................#..#......#....#..#..........#........ +..........................................................................#..........................................#.........#.. +.....#....#...........................#......#..........#........................#..........#.............#...........#..........# +................#.......................#...........#.............................#.....#..#..................................#... +........................................................................#..#.......................................#.............. +........#......................#.....#.......#.............#....#.........#....#........................#.............#........... +.............#....#.......#........#..#.......##................#.......................................#......................... +......#..............#.#...#...............#.........................#.....#............#..#.........#...............#............ +..................#...#..............................................#................#.......#................#.................. +....#...#.............#........................#....................................#......#................#......#.............. +..........................................................................................#...#.........##........................ +.............#.#...............#.....#.#......#.............................#.......................................#.....#....... +.................................................#................................................................#............... +........#.#......................#......#........#.......................................................#........................ +....#........#......#..................................................................................#............#............. +...........#.........#.......#...............#........................#........................................................#.# +........................##..................#........................#..........................................#.............#... +..........#........#........#.........#..................#.....#..............................#..#..............#................. +#..............................................................#..#............................#..........#....................... +.................................................................#...............#...#.#.........#.#.................#...........# +.....................................#.....................#.....................#........#....#....#............................. +#..#................#.....#....................................##............#...............#...#..........#..................... +.................#.....#.....................#................#...........#...................................................#... +..................................#........#.................#....#..................#.......................................#.... +..#....#............................#................................................##.........................................#. +...............#......................................................#......##................................................... +.........................#..................................#................##.#..#.#.................#.......#.................. +.................#......................................#...#..................................................................... +.........#..........................................................................................#.......#..................... +....##..#.........#..............#..#............#............#...................................#............................... +......#.......................................#................................................................#.................. +....#..................#............................#................................................................#............ +...............................##.#..............#....#............#.........................................#.....#.............. +........#........#....#.................................................................#.....#...................#............... +#...........#..#.........................................#..................................................#..##.......#.....#..# +................#......#...........##..........................................................#....#............................. +..........#..........#.............................................................#..............#.................#..........#.. +#.................#....................................................................................#..#..............#........ +.....#..............#.........................#.....#...........#......................#.......................................... +.....##.....................#...............................................................#..................................... +...................#.................................................................................#.....................#..#... +...............................................^......................#...........................#............................... +.......#...............#.........#.........#......#.......................................................................#....... +........#.........#..#..##.................................#.....#..............#...........#..#........#......................... +..............................................................................#......#.#...............#..#..#.....#....#......... +............#...#.......##............#....#...........................................................................#.......... +..................................##........#.....##.......#...................................................................... +.......................#...........................................#..................................#...#.........#............. +............#......................................................#....#..........#...................#........................#. +....................................................#......................#..........................#....#...................... +..................#.##....................................#..................................#.................#....#...#......... +........#.....................................................................................#...#............................... +....................................................#..........................................#........#..#.#.................#.. +....#...........#..#......#.........................#.....................#............................#.....#.#..............#... +.............................#.......#..........#..................................................................#....#.....#... +.....#.#................#...................#...............................................#..................................... +....#........................................#......#.............................#............................................... +.......#..........#........................#.....................................#..............#......#...##..................... +.....#..#.............................##........................................................#.....#..............#.#.......... +...........................................#......#......................................#..............#......................... +...................#.........#....#................#.....#.........#....................#............#..#........#...............# +.....#.....................#......................................................................#............................... +...............#.......................................#...#.................#..............#..................................... +.........................#........................................................#............................................... +..................................#..........................................#...........#.....................#.................. +.......................#.#............#.........................................#................................................. +..........................#...................#......................#......................................#..................... +.........#.........................#..............................................................#............................... +..#..............................#...............#..................................###..........................................# +.............................................#...............#.....#........................................................#..#.. +................#..........................#.......................................#........................................##.#.. +......##........#.....................................#............#..................#...............................#........... +..#..................#......................................#....#.......................#................................#....... +........................#...............#....#...#..........................................................................#..... +.............#.#..........#...#....#.................##........................................................................... +......#....#.......................................................................#.....................................#..#..... +.............#.......#............#...#..........#...............#..................................................#..........#.. +..........................#...........................................................#.............#............................. +#......................................#........#......#.......................................................................... +................#..........................................................#...................................................... +..................#................................................................#..........................#..........#.......# +.......#.....#............##..............................................................#....................#.....#......#..... +...........................................#..........#.#............................................#............................ +.......#.......................#..........................................................#....................................... +.................#........................#.........#...#............................................................#............ +................................................#...................#.........#...........#...........................#........... +..#..............#......#..............................##............#..................#.......#................................. +.#.#.........................................#......##.....##........#.............#..#..................##....#.#...#............ +..#.....................#.......#....#...............#...............#...................................................#........ +.#...#...............#..................................................................#..........#....#.................#.#..... +.#...................#...................................................................#.............#.......................#.. +.....................................#.....#..#...........#...........#..........#....................................#........... +.................................................................................#..#.............##....#...#............#........ +......................#....#........#......#...................#.............................................#.................... +.......................................#..........................#............................#.#..........................#..... +.#........................##....................................#.......#...........................#..#.......................#.. +.........#.........#................................................................#.................#........#.................. +..............#.......#.....#....................#........##.............#....#...............#.............#...........#...#..... +.................................................................#..#........#...#...#.........#.................................. +................#......##..........#..........#.........#....#............................................#.#.............#....... +...#...................................................................#.......................................#.................. +.......#..................#............#.....#................#..#....##.......#...............#.................................. +............#............##....#...................#..........................#......................................#...#...#.... +......#.....#..........##...............#......#...............................#......#........................................... +.................#.......#.......................................................#............#........#...#.......#...........#.. +......................##........#...................#.....#....................#...#....#........................#................ +.................#.....#.......#...........#.......#....................##....................................................#.#. +......#....................................#.........................#..................................#................#.#..#... +..............#...........................................................................................................#....... +#.#......#.................................................................................#...........#...#...................... +...........................................#.#........#....#............#.....................#................................... +...#.............#.....................................................................................................#.......... +..............#..........#............#...#..............#........................................##........#.....#.......#....... +..................##........#.............................................#.........................................###........... +..........#......................#............................................................................#....#.............. +.............#........#.............#.#...............#.....#..........#.....#....#...#...........#....#.......................... +........#...#.......#........................#.............#..........#........#.......................#.......................... +..................................................................#...............#......#...#.......#............................ +.............#..........#...................##.............#.........#..................#......................................... +.#.....#..#...#...............#.#.............#...........#....................................................................... +..............#..#.........................#..............#..#..............#.......................#.#........................... +............#.......................#....#................#..........#.......................#.....................#.............. +#.................#............................#...##....#.....#.....................................................#............ +.............................#........................#..................................................#....#................... +.##...........................................#.....#....#....#.......................#...#.....#...#............#................ +........#.............#.........#..............#........................#.....................#...#............................... diff --git a/solutions/01.ts b/solutions/01.ts index 29aa8c5..1843cc1 100644 --- a/solutions/01.ts +++ b/solutions/01.ts @@ -8,34 +8,33 @@ 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[] = [] left.forEach((left, idx) => { diff.push(Math.abs(left - right[idx])) }) - + const result = diff.reduce((prev, curr) => prev + curr) console.log(result) return this } 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 = 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 = 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 } } diff --git a/solutions/04-a.ts b/solutions/04-a.ts index 26d9071..0ee1110 100644 --- a/solutions/04-a.ts +++ b/solutions/04-a.ts @@ -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())) @@ -52,7 +52,7 @@ class DayFour implements Solvable { const vertical = countOccHorizontal(transpose(matrix)) console.log(horizontal + vertical + downRight + downLeft) - + return this } diff --git a/solutions/05.ts b/solutions/05.ts index 0b639c2..f0c9a2d 100644 --- a/solutions/05.ts +++ b/solutions/05.ts @@ -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) } }) diff --git a/solutions/06.ts b/solutions/06.ts new file mode 100644 index 0000000..ac9e31d --- /dev/null +++ b/solutions/06.ts @@ -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()) \ No newline at end of file diff --git a/utils.ts b/utils.ts index 137315f..d02d368 100644 --- a/utils.ts +++ b/utils.ts @@ -1,5 +1,5 @@ function readInput(day: string): string { - return Deno.readTextFileSync('./inputs/' + day + '.txt') + return Deno.readTextFileSync('./inputs/' + day + '.txt').trim() } export { readInput };