For a description of this problem, please check the Advent of Code website.
Example Input:
p=40,73 v=-96,64p=74,78 v=91,-65p=100,86 v=98,62p=61,29 v=95,-68p=50,53 v=-50,-63p=43,77 v=-42,-37p=47,44 v=34,66p=11,21 v=72,66p=9,61 v=-26,77p=46,100 v=-10,-82p=64,15 v=-5,-44p=50,97 v=-56,37p=37,35 v=-95,80p=33,8 v=60,39p=56,81 v=38,7p=60,22 v=-11,69p=47,36 v=-55,33p=29,51 v=-39,-69p=93,89 v=-96,-14p=67,51 v=-80,-78p=68,102 v=-46,46p=66,51 v=-9,40p=95,28 v=73,-1p=41,86 v=-97,81p=15,54 v=16,-33p=68,47 v=-7,-20p=75,34 v=40,23p=4,38 v=27,73p=14,78 v=-82,-49p=23,68 v=63,87p=82,69 v=83,24p=4,38 v=62,14p=60,65 v=46,71p=12,14 v=13,-31p=14,17 v=20,-67p=65,64 v=-4,-42p=2,19 v=-68,89p=4,12 v=-22,-81p=17,95 v=20,-20p=68,49 v=37,91p=37,1 v=-92,-68p=25,81 v=16,81p=96,60 v=-75,57p=57,37 v=-1,3p=21,65 v=49,71p=38,58 v=-93,-16p=98,52 v=77,-70p=66,85 v=91,54p=16,57 v=-79,80p=6,87 v=-27,-2p=27,60 v=56,34p=66,55 v=-52,-63p=57,59 v=31,39p=75,74 v=-10,34p=31,83 v=-32,-49p=95,34 v=-19,-7p=81,68 v=-63,-99p=43,68 v=63,14p=43,78 v=84,-90p=78,60 v=38,-86p=77,58 v=37,-23p=44,83 v=12,-39p=50,61 v=-99,-3p=91,51 v=24,73p=0,23 v=-67,19p=52,6 v=-50,42p=79,29 v=-66,96p=66,82 v=46,41p=21,27 v=-85,-34p=33,60 v=-95,7p=60,53 v=-58,30p=58,91 v=-34,-28p=49,16 v=1,-84p=7,2 v=-68,46p=99,97 v=75,-55p=95,39 v=37,46p=17,37 v=-55,59p=90,7 v=-19,85p=4,95 v=-88,23p=66,26 v=-6,-4p=33,19 v=-39,-4p=59,60 v=46,-13p=31,67 v=13,80p=70,102 v=-9,-18p=95,32 v=-72,-87p=73,40 v=-11,93p=8,17 v=-80,-34p=47,79 v=-48,51p=74,59 v=-73,79p=50,5 v=-51,72p=44,25 v=-89,86p=0,89 v=-67,-25p=12,81 v=43,59p=49,7 v=3,2p=25,99 v=-39,25p=42,91 v=-88,-48p=6,28 v=83,44p=53,57 v=-52,-23p=45,30 v=53,53p=98,4 v=26,65p=69,1 v=82,-48p=50,8 v=-99,-98p=2,11 v=86,-33p=58,29 v=-10,23p=25,97 v=72,42p=67,96 v=95,85p=61,97 v=97,38p=33,25 v=83,-50p=45,1 v=-45,95p=67,98 v=42,-78p=30,28 v=7,-45p=74,48 v=98,-30p=12,6 v=74,75p=53,5 v=48,-93p=13,97 v=78,-45p=96,10 v=-72,-41p=23,13 v=14,-21p=1,46 v=-24,10p=40,39 v=96,40p=14,98 v=21,55p=22,102 v=-99,-39p=78,98 v=-63,-48p=65,88 v=62,-74p=89,29 v=-64,89p=41,74 v=89,67p=81,51 v=90,69p=7,96 v=-27,35p=83,3 v=-64,-21p=72,52 v=94,-83p=43,54 v=-15,-31p=10,92 v=-27,48p=30,0 v=-87,28p=15,18 v=-9,63p=11,65 v=-30,84p=13,27 v=62,-47p=63,63 v=53,-9p=67,80 v=-14,15p=15,9 v=-98,64p=58,24 v=96,-34p=37,21 v=6,9p=57,56 v=50,84p=75,9 v=35,84p=48,1 v=-60,58p=70,51 v=47,34p=5,69 v=76,-9p=63,4 v=50,79p=25,17 v=-36,3p=87,42 v=16,2p=69,27 v=42,66p=8,42 v=70,-30p=0,52 v=87,2p=3,3 v=-23,75p=45,27 v=47,-77p=23,100 v=-13,-99p=69,19 v=-7,-4p=38,70 v=6,-89p=4,22 v=-28,49p=86,91 v=89,-2p=90,100 v=-32,56p=2,38 v=-81,99p=44,32 v=-96,-40p=96,77 v=51,-22p=5,71 v=93,-89p=90,89 v=24,30p=27,65 v=-89,-29p=33,56 v=10,67p=34,71 v=18,-47p=66,68 v=44,11p=55,37 v=-91,15p=73,6 v=-14,-91p=45,97 v=-54,-21p=34,88 v=34,-27p=65,14 v=67,-2p=40,5 v=-85,-14p=8,95 v=15,91p=5,35 v=-29,-57p=71,83 v=-11,34p=79,40 v=-6,-26p=62,78 v=-3,51p=12,15 v=-16,-25p=64,12 v=77,-99p=33,91 v=68,-45p=86,77 v=-24,-6p=55,27 v=83,-91p=85,63 v=38,40p=4,4 v=-74,-22p=50,19 v=-89,20p=45,20 v=-98,29p=11,50 v=19,-23p=43,82 v=5,-82p=72,14 v=40,65p=68,35 v=47,-7p=7,91 v=-78,45p=28,32 v=-87,41p=47,48 v=-51,4p=12,45 v=71,30p=12,76 v=83,-3p=74,21 v=-11,-27p=43,32 v=59,16p=21,12 v=-39,32p=76,88 v=92,68p=42,56 v=-44,-30p=27,76 v=65,-9p=61,2 v=-57,95p=42,77 v=-90,88p=27,74 v=-39,51p=55,36 v=-59,50p=3,9 v=82,-37p=64,78 v=-57,-62p=71,83 v=-51,94p=91,63 v=8,-29p=43,68 v=-46,25p=88,38 v=87,-96p=31,65 v=-35,-74p=13,86 v=-30,-95p=7,43 v=-31,-20p=65,89 v=-60,81p=10,55 v=69,48p=66,85 v=-34,14p=64,95 v=-99,62p=53,46 v=39,2p=79,3 v=-61,55p=56,50 v=-82,-21p=31,76 v=73,-92p=13,102 v=-34,-88p=31,100 v=-43,-98p=17,4 v=-82,-24p=45,53 v=55,-33p=61,24 v=8,-95p=75,96 v=-12,-78p=64,7 v=-55,-1p=82,27 v=-66,36p=50,63 v=-99,47p=38,63 v=44,-64p=62,27 v=3,-4p=78,40 v=67,20p=7,24 v=84,-67p=5,25 v=-28,-97p=33,101 v=-40,-45p=38,9 v=-94,-94p=76,13 v=-55,55p=48,47 v=21,-61p=95,6 v=-65,99p=12,51 v=69,-53p=88,4 v=82,-71p=41,76 v=-37,-12p=51,45 v=-11,4p=66,77 v=96,25p=92,100 v=33,5p=96,64 v=-29,78p=57,54 v=-60,-20p=22,33 v=71,49p=12,35 v=-27,-57p=88,17 v=97,-1p=38,61 v=61,20p=14,39 v=17,45p=73,70 v=68,4p=57,41 v=-45,-77p=73,35 v=-68,66p=95,70 v=-72,54p=51,44 v=-57,-73p=47,2 v=46,-68p=55,65 v=-59,-82p=71,15 v=-78,67p=5,96 v=72,-88p=10,30 v=73,-17p=89,84 v=-66,-82p=21,4 v=-36,-18p=15,44 v=67,43p=67,48 v=44,50p=70,16 v=7,-21p=62,5 v=-74,49p=60,52 v=96,90p=81,80 v=-57,-42p=2,86 v=-25,-98p=48,31 v=49,-97p=25,69 v=17,24p=68,7 v=-52,-5p=20,1 v=-30,-54p=95,25 v=55,68p=76,101 v=-8,98p=35,5 v=12,-28p=80,90 v=37,-91p=8,51 v=25,-53p=71,58 v=-51,-33p=36,19 v=-46,-80p=80,8 v=37,-38p=26,96 v=-80,14p=19,56 v=-34,-33p=58,93 v=12,66p=23,65 v=-28,70p=5,89 v=-77,-98p=99,67 v=-66,-29p=33,94 v=-94,52p=71,10 v=-7,-21p=67,38 v=45,30p=30,27 v=-92,-86p=40,55 v=-99,40p=25,78 v=-46,9p=98,0 v=-14,29p=21,51 v=70,60p=64,56 v=46,50p=83,80 v=20,-87p=32,60 v=-89,-86p=52,61 v=-52,-46p=49,20 v=-98,99p=19,61 v=19,17p=9,99 v=71,12p=9,63 v=-80,24p=16,0 v=18,-38p=7,18 v=24,-5p=89,46 v=-89,-11p=31,64 v=-86,-51p=60,13 v=54,-37p=83,46 v=-70,-16p=4,3 v=-25,-18p=45,67 v=79,56p=68,61 v=-15,-13p=13,64 v=-23,-56p=81,78 v=-92,-18p=1,71 v=-17,-49p=9,95 v=-64,28p=69,21 v=44,69p=25,74 v=-86,94p=71,1 v=34,2p=58,37 v=-10,-4p=32,81 v=-46,25p=21,87 v=-2,-92p=78,70 v=35,-96p=86,70 v=32,84p=42,68 v=72,-68p=76,69 v=36,-99p=72,79 v=69,-6p=92,22 v=-72,99p=74,9 v=-12,-94p=52,33 v=-43,45p=34,79 v=-41,-59p=26,56 v=10,50p=59,46 v=-22,23p=9,49 v=68,-59p=78,11 v=62,40p=88,25 v=43,34p=61,64 v=-53,27p=17,13 v=-32,-81p=100,45 v=26,70p=30,52 v=13,20p=9,93 v=-81,18p=68,3 v=93,15p=77,56 v=93,60p=48,5 v=-42,-77p=30,55 v=-87,-33p=42,87 v=6,78p=7,3 v=51,-8p=69,79 v=-61,30p=51,45 v=-49,35p=20,85 v=11,65p=25,58 v=17,-66p=45,2 v=-39,-5p=88,38 v=69,-98p=10,2 v=-81,32p=26,31 v=44,-25p=29,15 v=-67,52p=59,73 v=-54,21p=99,43 v=32,-48p=99,36 v=-71,13p=54,35 v=52,63p=44,77 v=-3,54p=32,82 v=-65,-50p=40,1 v=-46,85p=12,71 v=20,-69p=90,78 v=78,87p=18,52 v=62,-70p=40,23 v=30,63p=76,27 v=-75,76p=88,62 v=91,97p=31,88 v=64,-8p=95,33 v=96,-26p=21,63 v=16,-46p=35,15 v=6,-54p=73,22 v=-10,46p=43,11 v=63,95p=99,18 v=-66,-67p=0,71 v=74,44p=72,66 v=-8,14p=100,30 v=30,-7p=57,17 v=-41,73p=5,3 v=80,58p=77,100 v=84,78p=61,78 v=99,8p=59,51 v=45,-53p=85,6 v=78,-17p=49,56 v=-99,34p=25,7 v=64,2p=81,1 v=44,-1p=19,50 v=-80,13p=1,67 v=75,91p=22,63 v=65,64p=25,73 v=75,60p=73,64 v=-11,84p=27,13 v=14,49p=4,27 v=-77,3p=52,41 v=-7,30p=34,83 v=-84,35p=15,62 v=-81,87p=49,101 v=13,77p=60,52 v=98,-56p=95,61 v=-22,-96p=37,79 v=11,-98p=82,48 v=20,70p=28,57 v=63,70p=28,48 v=-43,4p=41,60 v=58,-89p=94,12 v=-27,-58p=22,66 v=-33,14p=14,47 v=19,86p=100,58 v=-70,-13p=1,17 v=1,76p=11,33 v=-54,93p=10,46 v=67,76p=69,11 v=50,-87p=37,12 v=-96,32p=1,23 v=69,33p=31,22 v=54,-44p=60,5 v=95,89p=6,84 v=25,-59p=2,22 v=76,89p=63,14 v=71,25p=54,33 v=-42,55p=89,34 v=-76,-50p=35,88 v=41,-34p=84,63 v=-12,67p=25,27 v=13,-70p=47,60 v=-95,17p=70,27 v=-5,24p=88,97 v=84,28p=47,52 v=50,-12p=24,99 v=66,-71p=35,67 v=7,64p=41,31 v=7,-79p=60,73 v=47,-82p=83,37 v=54,12p=14,45 v=19,-10p=81,80 v=12,-53p=92,51 v=38,-55p=14,26 v=-75,82p=89,38 v=-33,34p=34,74 v=56,47p=9,31 v=-35,-63p=62,98 v=-74,-62p=14,54 v=-68,29p=70,41 v=66,-55p=51,52 v=-57,34p=0,35 v=61,-49p=99,54 v=76,-26p=75,3 v=-82,-26p=43,95 v=7,28p=1,51 v=27,-66p=18,15 v=25,92p=5,53 v=78,-53p=58,95 v=49,-18p=34,49 v=-39,90p=19,75 v=60,91p=61,43 v=39,3p=74,51 v=-13,23p=32,26 v=3,66p=30,6 v=-33,99p=66,86 v=61,1p=25,87 v=45,-81p=22,76 v=-86,71p=90,12 v=32,99p=74,98 v=93,25p=16,94 v=68,58p=12,64 v=-4,-96p=87,8 v=-75,-4p=28,33 v=-81,40p=6,34 v=74,-44p=65,85 v=-56,41p=71,67 v=46,80p=73,85 v=-9,-12p=11,64 v=64,50p=73,46 v=48,-23p=62,22 v=4,31p=10,22 v=21,92p=73,67 v=-1,95p=97,2 v=30,-58p=96,42 v=-29,80p=26,53 v=63,20p=30,7 v=61,-11p=4,90 v=-77,-48p=23,55 v=58,14p=42,68 v=58,77p=65,68 v=60,-28p=77,87 v=-15,71p=40,14 v=-41,-21p=78,42 v=-20,-20p=29,95 v=71,76p=38,62 v=-40,67p=98,86 v=-25,38p=3,37 v=27,63p=41,70 v=39,21
Part 1 Solution:
const fs = require('fs');const input = fs.readFileSync('./example-input.txt', 'utf8').trim().split('\n');const robots = input.map(line => {const match = line.match(/p=(\d+),(\d+) v=(-?\d+),(-?\d+)/);return {px: parseInt(match[1], 10),py: parseInt(match[2], 10),vx: parseInt(match[3], 10),vy: parseInt(match[4], 10)};});const width = 101;const height = 103;let topLeft = 0;let topRight = 0;let bottomLeft = 0;let bottomRight = 0;robots.forEach(robot => {const finalX = (robot.px + (robot.vx * 100)) % width;const finalY = (robot.py + (robot.vy * 100)) % height;const wrappedX = finalX < 0 ? finalX + width : finalX;const wrappedY = finalY < 0 ? finalY + height : finalY;if (wrappedX === Math.floor(width / 2) || wrappedY === Math.floor(height / 2)) {return;}if (wrappedX < Math.floor(width / 2) && wrappedY < Math.floor(height / 2)) {topLeft++;} else if (wrappedX >= Math.floor(width / 2) && wrappedY < Math.floor(height / 2)) {topRight++;} else if (wrappedX < Math.floor(width / 2) && wrappedY >= Math.floor(height / 2)) {bottomLeft++;} else if (wrappedX >= Math.floor(width / 2) && wrappedY >= Math.floor(height / 2)) {bottomRight++;}});console.log(topLeft * topRight * bottomLeft * bottomRight);
Part 2 Solution:
const fs = require('fs');const input = fs.readFileSync('./example-input.txt', 'utf8').trim().split('\n');const robots = input.map(line => {const match = line.match(/p=(\d+),(\d+) v=(-?\d+),(-?\d+)/);return {px: parseInt(match[1], 10),py: parseInt(match[2], 10),vx: parseInt(match[3], 10),vy: parseInt(match[4], 10)};});const width = 101;const height = 103;const calculateDensity = (positions) => {let density = 0;const set = new Set(positions.map(({ x, y }) => `${x},${y}`));for (let y = 0; y < height; y++) {for (let x = 0; x < width; x++) {if (!set.has(`${x},${y}`)) {density++;}}}return density;};const simulateAndFindLowestDots = (maxSteps) => {let lowestDots = Infinity;let bestTime = 0;for (let time = 0; time < maxSteps; time++) {const positions = robots.map(robot => {const finalX = (robot.px + (robot.vx * time)) % width;const finalY = (robot.py + (robot.vy * time)) % height;const wrappedX = finalX < 0 ? finalX + width : finalX;const wrappedY = finalY < 0 ? finalY + height : finalY;return { x: wrappedX, y: wrappedY };});const dotsCount = calculateDensity(positions);if (dotsCount < lowestDots) {lowestDots = dotsCount;bestTime = time;}}console.log(bestTime);};simulateAndFindLowestDots(20000);