|
| 1 | +import { getPuzzle } from "../../utils"; |
| 2 | + |
| 3 | +const puzzleInput = getPuzzle(__dirname).trim(); |
| 4 | + |
| 5 | +const robots = puzzleInput.split("\n").map((line) => { |
| 6 | + const [initialPosition, velocity] = line |
| 7 | + .replace("p=", "") |
| 8 | + .replace("v=", "") |
| 9 | + .split(" ") |
| 10 | + .map((c) => c.split(",").map(Number)); |
| 11 | + |
| 12 | + return { |
| 13 | + position: { x: initialPosition[0], y: initialPosition[1] }, |
| 14 | + velocity: { x: velocity[0], y: velocity[1] }, |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +const WIDTH = 101; |
| 19 | +const HEIGHT = 103; |
| 20 | + |
| 21 | +const printRobots = (robotData: typeof robots) => { |
| 22 | + const robotMap: string[][] = [...Array(HEIGHT).keys()].map(() => []); |
| 23 | + |
| 24 | + for (let y = 0; y < HEIGHT; y++) { |
| 25 | + for (let x = 0; x < WIDTH; x++) { |
| 26 | + const matches = robotData.filter( |
| 27 | + (robot) => robot.position.x === x && robot.position.y === y |
| 28 | + ); |
| 29 | + |
| 30 | + robotMap[y][x] = `${matches.length || "."}`; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + console.log(robotMap.map((row) => row.join("")).join("\n")); |
| 35 | +}; |
| 36 | + |
| 37 | +// Part 1 |
| 38 | +(() => { |
| 39 | + console.time("part 1"); |
| 40 | + const TOTAL_SECONDS = 100; |
| 41 | + |
| 42 | + for (let i = 0; i < TOTAL_SECONDS; i++) { |
| 43 | + for (const robot of robots) { |
| 44 | + const newXPos = (robot.position.x + robot.velocity.x) % WIDTH; |
| 45 | + robot.position.x = newXPos >= 0 ? newXPos : WIDTH + newXPos; |
| 46 | + |
| 47 | + const newYPos = (robot.position.y + robot.velocity.y) % HEIGHT; |
| 48 | + robot.position.y = newYPos >= 0 ? newYPos : HEIGHT + newYPos; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + const middleX = Math.floor(WIDTH / 2); |
| 53 | + const middleY = Math.floor(HEIGHT / 2); |
| 54 | + |
| 55 | + const topLeftQuadrant = robots.filter( |
| 56 | + (robot) => robot.position.x < middleX && robot.position.y < middleY |
| 57 | + ).length; |
| 58 | + |
| 59 | + const topRightQuadrant = robots.filter( |
| 60 | + (robot) => robot.position.x > middleX && robot.position.y < middleY |
| 61 | + ).length; |
| 62 | + |
| 63 | + const bottomRightQuadrant = robots.filter( |
| 64 | + (robot) => robot.position.x > middleX && robot.position.y > middleY |
| 65 | + ).length; |
| 66 | + |
| 67 | + const bottomLeftQuadrant = robots.filter( |
| 68 | + (robot) => robot.position.x < middleX && robot.position.y > middleY |
| 69 | + ).length; |
| 70 | + |
| 71 | + const safetyFactor = |
| 72 | + topLeftQuadrant * |
| 73 | + topRightQuadrant * |
| 74 | + bottomRightQuadrant * |
| 75 | + bottomLeftQuadrant; |
| 76 | + |
| 77 | + console.log("part 1 safety factor ::", safetyFactor); |
| 78 | + console.timeEnd("part 1"); |
| 79 | +})(); |
| 80 | + |
| 81 | +// Part 2 |
| 82 | +(() => { |
| 83 | + console.time("part 2"); |
| 84 | + let seconds = 0; |
| 85 | + |
| 86 | + while (true) { |
| 87 | + seconds++; |
| 88 | + for (const robot of robots) { |
| 89 | + const newXPos = (robot.position.x + robot.velocity.x) % WIDTH; |
| 90 | + robot.position.x = newXPos >= 0 ? newXPos : WIDTH + newXPos; |
| 91 | + |
| 92 | + const newYPos = (robot.position.y + robot.velocity.y) % HEIGHT; |
| 93 | + robot.position.y = newYPos >= 0 ? newYPos : HEIGHT + newYPos; |
| 94 | + } |
| 95 | + |
| 96 | + const robotPositions = new Set( |
| 97 | + robots.map((robot) => `${robot.position.x},${robot.position.y}`) |
| 98 | + ); |
| 99 | + |
| 100 | + if (robotPositions.size === robots.length) { |
| 101 | + printRobots(robots); |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + console.log("part 2 seconds for tree ::", seconds); |
| 107 | + console.timeEnd("part 2"); |
| 108 | +})(); |
0 commit comments