diff --git a/2024/log.txt b/2024/log.txt new file mode 100644 index 0000000..e69de29 diff --git a/2024/log1.txt b/2024/log1.txt new file mode 100644 index 0000000..6165d8a --- /dev/null +++ b/2024/log1.txt @@ -0,0 +1,991 @@ + +1 +.....#..... +...##...... +......#.... +.#....#.... +........... +.##...#..#. +#...#...... + +2 +.....#..#.. +........... +..#........ +.#........# +....#...... +......#.... +..##....... + +3 +........... +#..#....#.. +...##..###. +#.......... +....#...#.. +....#...... +........... + +4 +........#.. +...#..#.... +........... +..........# +...#..#.... +..#.#...... +.#....#...# + +5 +........... +.....#..... +.#......... +.#..#...... +....##..##. +..#........ +..#..#..... + +6 +...#...#..# +.####...... +.##.....#.. +........... +......#.... +........... +#.......... + +7 +#....#.#... +........... +........... +##.#......# +.....#....# +..........# +#.......... + +8 +.........#. +..#....#... +.......#... +........#.# +........... +..#...#..## +...#...#... + +9 +.#.......#. +........... +.....#..... +.....#.#... +........#.. +.....#...#. +....#....#. + +10 +........... +....##.#... +#.......#.# +.#......... +#.......#.. +.......#... +........... + +11 +.........#. +.......#.#. +........... +..#........ +.......#..# +#.....#.... +#.##....... + +12 +........... +.#......... +....##..... +...##...... +###...#.... +......#.... +......#..#. + +13 +..#...#.... +...#.##.#.. +.#...#....# +........... +..#........ +........... +....#...... + +14 +#..#....#.. +........... +........... +...##..#... +........##. +#.......... +....#...... + +15 +..#........ +.#........# +........#.. +...##...... +........... +...#..#...# +......#...# + +16 +..#..#..... +........... +........#.. +....#....#. +.#......... +.#..#...... +.....#..... + +17 +........... +#.#.....#.. +.#.##.##... +..#........ +.#.#....... +..........# +........... + +18 +..........# +##......... +........... +.....#..... +#..#....... +.......#..# +#....#.#..# + +19 +........... +........#.. +.......#.#. +..#....#... +...#..##.#. +..........# +..#.......# + +20 +.....#...#. +.#..#....#. +.....#.#.#. +........... +.........#. +........... +........#.. + +21 +#...##....# +........... +........... +#......##.. +#.......#.. +.#......... +........#.. + +22 +......#.... +#.#........ +.........#. +#......#... +........... +...#...#..# +..#......#. + +23 +......#..#. +........... +#.......... +.##........ +.....#..... +...##...... +.#....#.... + +24 +........... +.#..#.....# +..#..##.#.. +...#....... +.....##.... +..#........ +........... + +25 +#.......... +....#...... +........... +........#.. +....#..#... +...#....... +#.......##. + +26 +........... +....#...... +..#.......# +.#........# +......#.#.# +...#....... +...#..#.... + +27 +.#..#...#.. +.##..#..... +..#.#....#. +........... +.....#..... +........... +.#......... + +28 +..##..#.#.. +........... +........... +##..#.....# +...#...#... +..#........ +.#......... + +29 +..........# +.....#....# +..........# +#......#... +........... +#..#...#... +.#...#..... + +30 +..#.......# +........... +...#....... +......#..#. +.........#. +..#....#... +.......##.. + +31 +........... +.....#.##.. +.#...#...#. +....#...... +.........#. +.....#..... +........... + +32 +.#......... +.......##.. +........... +#.......... +#.......#.. +.......#..# +#...##..#.. + +33 +........... +#.......... +..#...#.... +#.#........ +...#.....#. +.......#... +.......#..# + +34 +#..##...... +....#.#..#. +.##...#.... +........... +.#......... +........... +.....#..... + +35 +.##...#...# +........... +........... +..#.##..#.. +......#.... +...#....... +.....#..... + +36 +...#....... +........##. +#.......... +...##...... +........... +#...#..##.. +....#...#.. + +37 +...#..#.... +........... +......#.... +......#...# +..#........ +.#........# +....#...#.. + +38 +........... +.#..#....#. +.##.##..... +.....#..... +.##........ +........#.. +........... + +39 +..#........ +.#........# +........... +...#....... +.#..#...... +#.....#.... +..##...##.. + +40 +........... +.......#... +.....#....# +.....#....# +##.....#..# +#.......... +#..#....... + +41 +..##...#... +..#....#..# +......#..## +........... +........#.. +........... +.........#. + +42 +.....#.#.#. +........... +........... +.#...#..##. +.....#...#. +....#...... +.........#. + +43 +.......#... +#.......#.. +.#......... +........#.# +........... +#...##..#.. +#......#... + +44 +.......#..# +........... +.........#. +...#....... +......#.... +#.#........ +#........#. + +45 +........... +.##..#..... +.#.##.#..#. +......#.... +....#.#.... +#.......... +........... + +46 +...#....... +..#..#..... +........... +......#.... +.....#..#.. +..#.#...... +.#....#...# + +47 +........... +...#....... +...#....#.. +........##. +#...#...#.. +....#...... +....#..#... + +48 +.#....#...# +...#..#.#.# +...#..#...# +........... +....#...... +........... +..#........ + +49 +.#..##...#. +........... +........... +.##..#..#.. +.#..#...... +.....#..... +..#........ + +50 +#.......... +...#...#... +..#........ +.#....#.... +........... +.##.#...#.. +...#......# + +51 +#..#....... +........... +.#......... +#......#... +..........# +.....#....# +.......#..# + +52 +........... +......#..#. +..#....##.# +.......#... +.......#..# +...#....... +........... + +53 +....#...... +.....#...#. +........... +.........#. +.#.......#. +........##. +.....#.#.#. + +54 +........... +..........# +#......#... +#.......#.. +.#..##.#... +........#.. +#.......#.. + +55 +#.#......#. +..#....#.## +...#...#... +........... +#.......... +........... +......#.... + +56 +.##.#...... +........... +........... +#....##..#. +...##...... +......#.... +......#.... + +57 +....#...... +......#.... +...#....... +..#..#..... +........... +.#...#..#.# +..#...#.... + +58 +....#..#... +........... +....#...... +#.......#.. +...#....... +........##. +#..#....... + +59 +........... +..#...#...# +.#.##.#...# +........#.. +...#......# +......#.... +........... + +60 +.....#..... +..#.....#.. +........... +.#......... +..#..#..... +.#...#..... +.#..#....#. + +61 +........... +......#.... +#..#....... +...#...#... +..#.....#.# +.#......... +.#..#...... + +62 +.#...#....# +#..#.#....# +#......#... +........... +.......#... +........... +..........# + +63 +......####. +........... +........... +..##.....## +..#....#... +.......#... +..........# + +64 +........#.. +.....#...#. +....#...... +.........#. +........... +.#...#.#.#. +.....#...#. + +65 +#.......#.. +........... +.......#... +....##..... +.......#... +#.......#.. +.#........# + +66 +........... +...#..#.... +#.#....#..# +.........#. +..#....#... +.........#. +........... + +67 +......#.... +#.....#.... +........... +....#...... +......#..#. +.#...#..... +.####...... + +68 +........... +..#........ +....#.#.... +......#.... +.###......# +.....#..... +.....#..#.. + +69 +....#...##. +#...#..##.. +#...#...#.. +........... +...#....... +........... +...#....... + +70 +....#.#...# +........... +........... +..##..#.... +.#........# +........#.. +...#....... + +71 +.#......... +.#..#...... +.....#..... +..#..#..... +........... +..#.##...#. +.#......#.. + +72 +.#..#...... +........... +..........# +..#.....#.. +#.......... +...#...#... +..#...#.... + +73 +........... +#......#..# +#..#.#.#..# +..........# +#....#..... +.#......... +........... + +74 +.......#... +...#......# +........... +.......#... +..#.......# +........##. +..#...##.#. + +75 +........... +.........#. +........##. +.....#...#. +....##.#... +.........#. +.#.......#. + +76 +#......##.. +##......#.. +....##..#.. +........... +..........# +........... +.......#... + +77 +#.##....... +........... +........... +......##.## +#.#........ +.........#. +.......#... + +78 +.....#..... +...##...... +......#.... +.#....#.... +........... +.##...#..#. +#...#...... + +79 +.....#..#.. +........... +..#........ +.#........# +....#...... +......#.... +..##....... + +80 +........... +#..#....#.. +...##..###. +#.......... +....#...#.. +....#...... +........... + +81 +........#.. +...#..#.... +........... +..........# +...#..#.... +..#.#...... +.#....#...# + +82 +........... +.....#..... +.#......... +.#..#...... +....##..##. +..#........ +..#..#..... + +83 +...#...#..# +.####...... +.##.....#.. +........... +......#.... +........... +#.......... + +84 +#....#.#... +........... +........... +##.#......# +.....#....# +..........# +#.......... + +85 +.........#. +..#....#... +.......#... +........#.# +........... +..#...#..## +...#...#... + +86 +.#.......#. +........... +.....#..... +.....#.#... +........#.. +.....#...#. +....#....#. + +87 +........... +....##.#... +#.......#.# +.#......... +#.......#.. +.......#... +........... + +88 +.........#. +.......#.#. +........... +..#........ +.......#..# +#.....#.... +#.##....... + +89 +........... +.#......... +....##..... +...##...... +###...#.... +......#.... +......#..#. + +90 +..#...#.... +...#.##.#.. +.#...#....# +........... +..#........ +........... +....#...... + +91 +#..#....#.. +........... +........... +...##..#... +........##. +#.......... +....#...... + +92 +..#........ +.#........# +........#.. +...##...... +........... +...#..#...# +......#...# + +93 +..#..#..... +........... +........#.. +....#....#. +.#......... +.#..#...... +.....#..... + +94 +........... +#.#.....#.. +.#.##.##... +..#........ +.#.#....... +..........# +........... + +95 +..........# +##......... +........... +.....#..... +#..#....... +.......#..# +#....#.#..# + +96 +........... +........#.. +.......#.#. +..#....#... +...#..##.#. +..........# +..#.......# + +97 +.....#...#. +.#..#....#. +.....#.#.#. +........... +.........#. +........... +........#.. + +98 +#...##....# +........... +........... +#......##.. +#.......#.. +.#......... +........#.. + +99 +......#.... +#.#........ +.........#. +#......#... +........... +...#...#..# +..#......#. + +100 +......#..#. +........... +#.......... +.##........ +.....#..... +...##...... +.#....#.... + +101 +........... +.#..#.....# +..#..##.#.. +...#....... +.....##.... +..#........ +........... + +102 +#.......... +....#...... +........... +........#.. +....#..#... +...#....... +#.......##. + +103 +........... +....#...... +..#.......# +.#........# +......#.#.# +...#....... +...#..#.... + +104 +.#..#...#.. +.##..#..... +..#.#....#. +........... +.....#..... +........... +.#......... + +105 +..##..#.#.. +........... +........... +##..#.....# +...#...#... +..#........ +.#......... + +106 +..........# +.....#....# +..........# +#......#... +........... +#..#...#... +.#...#..... + +107 +..#.......# +........... +...#....... +......#..#. +.........#. +..#....#... +.......##.. + +108 +........... +.....#.##.. +.#...#...#. +....#...... +.........#. +.....#..... +........... + +109 +.#......... +.......##.. +........... +#.......... +#.......#.. +.......#..# +#...##..#.. + +110 +........... +#.......... +..#...#.... +#.#........ +...#.....#. +.......#... +.......#..# +Christmas tree found! diff --git a/2024/src/2024/day14/day14.test.ts b/2024/src/2024/day14/day14.test.ts index 637cdc2..1f6098e 100644 --- a/2024/src/2024/day14/day14.test.ts +++ b/2024/src/2024/day14/day14.test.ts @@ -8,8 +8,7 @@ describe('2024 Day 14', () => { }); test('Part 2', async () => { - expect(await part2('testInput1', 11, 7)).toEqual(31); - expect(await part2('input', 101, 103)).toEqual(29379307); + await part2('input'); }); }); diff --git a/2024/src/2024/day14/day14.ts b/2024/src/2024/day14/day14.ts index 6526f29..61876eb 100644 --- a/2024/src/2024/day14/day14.ts +++ b/2024/src/2024/day14/day14.ts @@ -1,13 +1,15 @@ import path from "node:path"; import {readInputLineByLine} from "@utils/io"; import {Coord} from "@utils/grid"; +import * as process from "node:process"; +import * as fs from "node:fs"; export async function part1(inputFile: string, hiX: number, hiY: number) { return await day14(inputFile, hiX, hiY, calcSafetyFactor); } -export async function part2(inputFile: string, hiX: number, hiY: number) { - return await day14(inputFile, hiX, hiY); +export async function part2(inputFile: string) { + return await day14(inputFile, undefined, undefined, keepMovingUntilChristmasTreeIsFound); } export type Robot = { @@ -15,7 +17,7 @@ export type Robot = { velocity: Coord; } -async function day14(inputFile: string, hiX: number, hiY: number, calcFn?: (robots: Robot[], hiX: number, hiY: number) => number) { +async function day14(inputFile: string, hiX?: number, hiY?: number, calcFn?: (robots: Robot[], hiX?: number, hiY?: number) => number | void) { const inputPath = path.join(__dirname, inputFile); const lines = await readInputLineByLine(inputPath); @@ -28,22 +30,15 @@ async function day14(inputFile: string, hiX: number, hiY: number, calcFn?: (robo return calcFn?.(robots, hiX, hiY); } -function calcSafetyFactor(robots: Robot[], hiX: number, hiY: number) { - const newRobotsPositions: Robot[] = []; - - robots.forEach(robot => { - const finalPosition = moveRobot(robot, 100, hiX, hiY); - newRobotsPositions.push({ position: finalPosition, velocity: robot.velocity }); - }); - +function calcSafetyFactor(robots: Robot[], hiX?: number, hiY?: number) { let q1Count = 0; let q2Count = 0; let q3Count = 0; let q4Count = 0; - const midX = Math.round(hiX / 2) - 1; - const midY = Math.round(hiY / 2) - 1; + const midX = Math.round(hiX! / 2) - 1; + const midY = Math.round(hiY! / 2) - 1; - newRobotsPositions.forEach(robot => { + moveAllRobots(robots, hiX, hiY).forEach(robot => { const { x, y } = robot.position; if (x < midX && y < midY) { q1Count++; @@ -58,6 +53,48 @@ function calcSafetyFactor(robots: Robot[], hiX: number, hiY: number) { return q1Count * q2Count * q3Count * q4Count; } +function keepMovingUntilChristmasTreeIsFound(robots: Robot[]) { + const hiX = 101; + const hiY = 103; + + const logStream = fs.createWriteStream('log.txt', { flags: 'w' }); + const log = (msg: string) => logStream!.write(`${msg}\n`); + + let seconds = 1; + let movedRobots = moveAllRobots(robots, hiX, hiY, 1); + + while (true) { + // logStream!.write('\n'); + // log(seconds.toString()); + + const robotPositions = new Set( + movedRobots.map(robot => `${robot.position.x},${robot.position.y}`) + ); + + for (let y = 0; y < hiY; y++) { + let line = ''; + for (let x = 0; x < hiX; x++) { + line += robotPositions.has(`${x},${y}`) ? '#' : '.'; + } + + if (line.includes("##############################")) { + console.log("Christmas tree found!"); + console.log(`after ${seconds} seconds`); + break; + } + } + + if (seconds >= hiX * hiY) { + break; + } + + movedRobots = moveAllRobots(movedRobots, hiX, hiY, 1); + seconds++; + } + + logStream!.end(); +} + export function moveRobot(robot: Robot, seconds: number, hiX: number, hiY: number) { const { x: posX, y: posY } = robot.position; const { x: vX, y: vY } = robot.velocity; @@ -68,4 +105,14 @@ export function moveRobot(robot: Robot, seconds: number, hiX: number, hiY: numbe const wrap = (value: number, max: number) => ((value % max) + max) % max; return new Coord(wrap(posX + vX * seconds, hiX), wrap(posY + vY * seconds, hiY)); +} + +function moveAllRobots(robots: Robot[], hiX: number | undefined, hiY: number | undefined, seconds = 100) { + const newRobotsPositions: Robot[] = []; + + robots.forEach(robot => { + const finalPosition = moveRobot(robot, seconds, hiX!, hiY!); + newRobotsPositions.push({position: finalPosition, velocity: robot.velocity}); + }); + return newRobotsPositions; } \ No newline at end of file