diff --git a/day-6/day6.test.ts b/day-6/day6.test.ts new file mode 100644 index 0000000..8395f83 --- /dev/null +++ b/day-6/day6.test.ts @@ -0,0 +1,37 @@ +import { expect, test } from "bun:test"; +import * as path from "node:path"; +import { day6, day6part2 } from "./day6.ts"; + +test("day 6, part 1", async () => { + const testInput = await Bun.file( + path.resolve(__dirname, "test-input.txt"), + ).text(); + const input = await Bun.file(path.resolve(__dirname, "input.txt")).text(); + + const testResult = day6(testInput); + const finalResult = day6(input); + console.log("\n\n"); + console.log("Test data:", testResult); + console.log("Full data:", finalResult); + console.log("\n\n"); + + expect(testResult).toEqual(41); + expect(finalResult).toEqual(5153); +}); + +test("day 6, part 2", async () => { + const testInput = await Bun.file( + path.resolve(__dirname, "test-input.txt"), + ).text(); + const input = await Bun.file(path.resolve(__dirname, "input.txt")).text(); + + const testResult = day6part2(testInput); + console.log("\n\n"); + console.log("Test data:", testResult); + expect(testResult).toEqual(6); + const finalResult = day6part2(input); + console.log("Full data:", finalResult); + expect(finalResult).toEqual(1711); + + console.log("\n\n"); +}); diff --git a/day-6/day6.ts b/day-6/day6.ts new file mode 100644 index 0000000..ff68e37 --- /dev/null +++ b/day-6/day6.ts @@ -0,0 +1,194 @@ +enum Direction { + UP = 0, + DOWN = 1, + LEFT = 2, + RIGHT = 3, +} + +type Coordinates = [number, number]; + +type Grid = string[][]; + +function getNextCellCoordinates( + coordinates: Coordinates, + direction: Direction, +): Coordinates { + const [x, y] = coordinates; + if (direction === Direction.UP) { + return [x - 1, y]; + } + if (direction === Direction.DOWN) { + return [x + 1, y]; + } + if (direction === Direction.LEFT) { + return [x, y - 1]; + } + return [x, y + 1]; +} + +function isPathFinished( + coordinates: Coordinates, + height: number, + width: number, +) { + const [x, y] = coordinates; + + if (x === -1 || y === -1 || x === height || y === width) { + return true; + } + + return false; +} + +function getNewDirection(prev: Direction) { + switch (prev) { + case Direction.LEFT: + return Direction.UP; + case Direction.RIGHT: + return Direction.DOWN; + case Direction.DOWN: + return Direction.LEFT; + case Direction.UP: + return Direction.RIGHT; + } +} + +export function day6(input: string) { + const grid = input.split("\n").map((line) => line.split("")); + const HEIGHT = grid.length; + const WIDTH = grid[0].length; + + let direction = Direction.UP; + let startCoords: Coordinates | null = null; + const path: Coordinates[] = []; + + for (let i = 0; i < grid.length; i++) { + const line = grid[i]; + for (let j = 0; j < line.length; j++) { + const cell = line[j]; + if (cell !== "^") { + continue; + } + startCoords = [i, j]; + break; + } + } + + if (!startCoords) { + throw new Error("no starting point"); + } + + path.push(startCoords); + + let isFinished = false; + + while (!isFinished) { + const prev = path.at(-1); + if (!prev) { + throw new Error("never"); + } + const [nextX, nextY] = getNextCellCoordinates(prev, direction); + isFinished = isPathFinished([nextX, nextY], HEIGHT, WIDTH); + if (isFinished) { + break; + } + + const nextCellContent = grid[nextX][nextY]; + + if (nextCellContent === "#") { + direction = getNewDirection(direction); + } else { + path.push([nextX, nextY]); + } + } + + const filtered = path.filter( + (o, index, arr) => + arr.findIndex((item) => item[0] === o[0] && item[1] === o[1]) === index, + ); + return filtered.length; +} + +export function day6part2(input: string) { + let count = 0; + const grid = input.split("\n").map((line) => line.split("")); + + for (let i = 0; i < grid.length; i++) { + const line = grid[i]; + for (let j = 0; j < line.length; j++) { + const newGrid = structuredClone(grid); + const currentValue = newGrid[i][j]; + if (currentValue === "#" || currentValue === "^") { + continue; + } + newGrid[i][j] = "#"; + const isLoop = checkInput(newGrid); + if (isLoop) count++; + } + } + return count; +} + +export function checkInput(grid: Grid) { + const HEIGHT = grid.length; + const WIDTH = grid[0].length; + + let direction = Direction.UP; + let startCoords: Coordinates | null = null; + const path: Coordinates[] = []; + const seen = new Set(); + + for (let i = 0; i < grid.length; i++) { + const line = grid[i]; + for (let j = 0; j < line.length; j++) { + const cell = line[j]; + if (cell !== "^") { + continue; + } + startCoords = [i, j]; + break; + } + } + + if (!startCoords) { + throw new Error("no starting point"); + } + + path.push(startCoords); + const s = sc(startCoords, direction); + seen.add(s); + let isFinished = false; + + while (!isFinished) { + const prev = path.at(-1); + if (!prev) { + throw new Error("never"); + } + const [nextX, nextY] = getNextCellCoordinates(prev, direction); + isFinished = isPathFinished([nextX, nextY], HEIGHT, WIDTH); + if (isFinished) { + isFinished = true; + + break; + } + + const nextCellContent = grid[nextX][nextY]; + + if (nextCellContent === "#") { + direction = getNewDirection(direction); + } else { + const s = sc([nextX, nextY], direction); + const x = seen.has(s); + + if (x) { + return true; + } + seen.add(s); + path.push([nextX, nextY]); + } + } +} + +function sc(c: Coordinates, dir: Direction) { + return [...c, dir].join(","); +} diff --git a/day-6/input.txt b/day-6/input.txt new file mode 100644 index 0000000..beeded5 --- /dev/null +++ b/day-6/input.txt @@ -0,0 +1,130 @@ +.......................#...................#....#..#......#.................#.#..#........................#.#..............#.....# +................#.#.......#..#.......#.................................................................................#.##....... +............#..............#......................#...#.....#...#..........##.........#.....#..................................... +..#...#......................................#............................#..................#..........#..#...................... +....#........#.#....................#..........#.....#.............#..#............................#..................#......#.... +..........................................................#................#............................................#......... +..#............................#.....#...........................#...........#........##...........................#..#........... +.....................#...........................#.#..............#......................................#.........#....#.....#... +.#...#.#.................#....................#...............................................................#.#..........#...... +....#....................................#...........................................#.....#..........#........................... +.......................#..........#..............#......#....#.......#........................................#.#................. +......#...........................................................#...#............#.................#............................ +.......#.................#..#.....................#............................................................................... +..............#....................#.#..........#........#......#................#.................#..........#................... +#.#.......................................#.........................#...................................#......................... +...#.........................................................................................#................#..................# +#.....#....#............................................................#.........................#..................#.#.......... +.#.....#.#.......#.#..................#.#......................#.......#.....#..#..................##.....#.......#...#........... +....#.........#.................................#..........#........#...............#................#...................#....#... +.................#...............#...#.................#.................................................#........................ +....#...#...............................#..................................................#..........#................#.....#.... +...........#........#....#.......#...#..........#...............#.....#.....#.....#..........#.................................... +............#.........#.................#................#.................#.#..................................................#. +............#......................................#...................................................................#.......... +...........#....#.....................................................................................#............#..........#... +..........#........................................#.............#.................#........................#..#.......#........#. +......#.............................#.................##..................................#.....................#................. +.#.................................#.............................................#.................##.....#..............#......#. +.........................#..............................#..........#...........#....#................#......................#..... +.......#...................#......##........................#.........#........................................................... +......................................................................##...........#................#............................. +....#...................................##...............................................#........................................ +...##.....#..#........#.................#..............................................................#.......................... +........##......#....................#..............................#......................#...................#.................. +.........................................................#.......................................................#.#.............. +..........#.............................#.........#...............#................#.#.........#..........................#.#..... +....................#...................#..#.......................................................#..........#...............#... +...........................................................#........................#.........#........#.......................... +.......#..............#......#.................................#...................##...........................#.........#....... +#...#...............................................................................................................#............. +................................#..#....#............#...........#..................#.............................#............... +....................#..............#.........#.................................#.............#..##................................ +#...#..................................................................................#.....#...........#........................ +........#...........#........#...#..................#.....#....#.........#.........................#...........................#.. +.............#...#...................................................................#...........#................................ +.............#........................................#...#.............#..............................#..#.........#............. +...#.................................................................................#.............#..........................##.. +...#.............................................................................................................................. +................#.........#..................................#...................#................................................ +...#................#...#.#.....................#...........#..................................#........#....#......#............. +..........................#.....................#..#...................................##........................................# +#........................#...............................................#........#..#..........#...........................#..... +.#............#.#..............................................................#...........#...................................... +......#..................................................................#.......#..............................#...#....#........ +........#........................#.........#.............#.....#.................................................................. +...............................................................................#...........................................#...... +........................#..................................#.....................................................#................ +............................................................#..........................#.................#.......................# +.....................#..#.........................#..............#.............................#........#..............##......#.. +.....................#.................................................#.......#............#........................#.......#.... +......................#.....................................................#......#............................#................. +................................................................#...#..##..#.....................................#...............# +..............#..............................................................................................#.................... +............................................#......#..........#...............................................................#... +....#.................................#..................................................................#..........##..#......... +............#.....#................................................................................#..........#.........#......... +.....................#......................................#.........#....................................................#....#. +.........................#...............................#...............................#....................#................... +.#.....................#.#.......................................#....#...........#.......................#....................... +.....................................................#.#..#......................................................................# +#..........#..........................#............................#.............#..................#.............#...........#... +........#......................................................................................................................... +..........#.............#.........................................#................................................#.............. +...........#................................#.................#...........................#.............................#......... +..................#.........................................#.#..................#...#...........#.......#..#..................... +........................................................#...#.......................................................#............. +.........................................................................#.......#..#.....#....................................... +............#..............................................................#.........#..............#.............#............... +#...#..........................................#...........#.....#.....................................#...............#.......... +...........................#............#......................................................................................... +...#..........................................#.......................................................................#........... +..............#......#.....#.....................#..........................#..................................................... +.........................#....#...................#...#.......#..............................#........................#........... +.............................#............^.#................................................#......#..#.#...........#............ +....................................................................................#...#.#...................................#... +.......................#..............................##..............................................#........................... +......................#...................................................#..........................................#............ +......#........................................................................#.....................................#............ +.......................#.............................#........................#...........#...........#............#...........#.. +....#...............#..#......#....#..................#.............#.......#...............#.......................#............. +............#...#..................#.............................................................................................. +....#.......#.................#.......................................................#..#......#................................. +.........#......#..........#......................#.....................#..................#.#.................................... +...............##.......................................................#...#.............................#.......#............... +........................................................##...................#...#............#......#............................ +...........#...#..............#.............................................................#...................#............#.... +...............................................#.....#.....#...................................................................... +.#....................#...#........#....................#.#.........#..........#................................#................# +.............#.....#..#....................................#.#...........#.................................................#..#.#. +......#.....................................#....................................................................#................ +............................................................##......#.....................#.......#.....................#...#..... +.#.......#......................#................................................................................................. +................#....#......................#.........#...................#........#.......##.....#............................... +..#....#....................#............................................................#.#......#............#.....#.#....#..... +.................................#.................................................#.......#...................................... +..................#...........#....#.....................#......#...............#..................#....................##........ +.##...............................................#............#...........................#...................................... +.............#...........#...........................#..#.......#...#..........#......................#..........#................ +.......#...................................#.................#....#..............................................................# +........#....................................................#.................#.....#.........#.#................................ +................#.................................#.....................#.....#.....#............................##............... +..............................................##.................#..##........#...........#............#.......................... +....#......................................#...............................................................................#...... +..#..............#.#................#..#..............#.......................................#.#.......#..............#......#..# +.....#..............................................................................................................#.......#..... +..#.......#.#..........#.......#..................................#..........#.......#.................#...........#...........#.. +...............#.....#................#............#................#............#.....#.#........#............................... +....#.....................#................#.....................................#......................#.#.......#....#.......... +........#...............#...#.....................................................#........#.........#.........#.................. +...#......#..........................#.....#..........................##.......#....#............................................. +.............#................................#............................#.....#.......#......#................................# +.......................#............#..........................#.................................................................. +.................................................#..............#...#...........................#................................. +......#...................#..............................................................................................#....#..# +....................................#......#.....................#..............#.......#...................#..................... +............#...................................#...........#........#........#...........................................##...... +#...........#..........................#....................................#.........#................................#.......... +......##..#...............................................................#...............................#.........#............. +.......................#.....#.........................#...#.........................#.....#..........#.#...#..................... +.......................#..#.....#.....#..........................#.........#.....#....................#.....#....................# \ No newline at end of file diff --git a/day-6/test-input.txt b/day-6/test-input.txt new file mode 100644 index 0000000..b60e466 --- /dev/null +++ b/day-6/test-input.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... \ No newline at end of file