Skip to content

Instantly share code, notes, and snippets.

@hyunwoona
Created December 15, 2024 22:50
Show Gist options
  • Save hyunwoona/44e306cdd6d55c169d5500f09d445297 to your computer and use it in GitHub Desktop.
Save hyunwoona/44e306cdd6d55c169d5500f09d445297 to your computer and use it in GitHub Desktop.
Advent of code 12/14/24
const map =
[
[44,70,-47,36],
[56,4,69,91],
[6,30,-52,-60],
[64,4,35,44],
[57,22,-56,48],
[5,22,18,37],
[2,48,88,-65],
[80,54,28,-40],
[76,0,98,4],
[7,89,33,-33],
[25,30,-15,70],
[92,41,35,80],
[39,6,-47,-20],
[30,82,97,8],
[8,67,-44,-59],
[87,24,-51,83],
[83,33,43,73],
[95,59,50,90],
[10,69,-47,-44],
[15,15,91,98],
[73,21,-24,77],
[11,22,2,-82],
[87,95,65,-42],
[26,47,33,-20],
[50,58,69,-62],
[88,60,12,68],
[82,15,-97,10],
[47,83,81,-93],
[96,92,47,-61],
[1,30,18,84],
[21,51,49,50],
[49,18,-70,-88],
[52,0,54,39],
[46,30,61,97],
[13,17,10,-51],
[72,85,21,-45],
[99,57,42,49],
[96,26,74,8],
[79,31,-66,55],
[0,80,-3,29],
[19,89,56,-45],
[13,85,-58,-59],
[34,51,-84,-38],
[67,96,-10,-42],
[75,1,-11,23],
[30,54,85,25],
[95,66,81,79],
[19,77,87,82],
[32,29,-85,-22],
[52,73,14,52],
[82,84,43,93],
[74,88,66,-99],
[47,18,-71,-31],
[98,71,2,36],
[28,70,24,-83],
[91,42,-90,46],
[54,101,69,-53],
[33,77,-34,70],
[0,64,95,98],
[56,63,-17,-37],
[43,88,-94,47],
[31,67,43,72],
[72,26,-96,-14],
[52,6,-56,-96],
[41,67,39,6],
[15,18,-14,-63],
[73,60,23,-79],
[20,54,95,97],
[66,41,96,-36],
[80,0,56,-71],
[83,41,-11,43],
[59,0,44,-64],
[92,20,-35,-6],
[85,102,44,-73],
[24,20,-87,-6],
[99,52,24,-66],
[40,54,-32,84],
[86,53,-10,-5],
[69,43,-80,32],
[81,95,98,15],
[55,76,-76,36],
[93,68,28,85],
[52,6,38,-96],
[76,22,-74,-17],
[7,93,63,67],
[83,12,18,51],
[42,100,-38,37],
[47,22,-39,10],
[85,7,-19,-12],
[39,5,26,-67],
[26,38,-38,-63],
[95,34,35,-33],
[67,78,-96,-17],
[51,52,92,-19],
[14,91,-6,-83],
[22,83,17,-42],
[26,38,-55,-73],
[84,76,-50,-48],
[15,42,72,3],
[68,32,-19,-63],
[40,39,32,49],
[35,87,48,17],
[32,81,43,5],
[66,71,93,40],
[1,59,80,68],
[99,60,38,71],
[66,29,44,32],
[13,18,-29,59],
[27,39,-84,5],
[21,96,-53,-64],
[49,30,-55,-79],
[36,51,-81,53],
[38,6,-31,-4],
[4,79,-84,-34],
[1,1,25,-85],
[28,46,-89,62],
[79,38,-8,-8],
[24,84,-45,-75],
[55,23,-71,-69],
[55,7,90,69],
[37,34,39,16],
[62,70,83,-32],
[99,84,-84,45],
[75,83,28,9],
[15,41,55,92],
[73,99,-64,56],
[65,3,4,16],
[17,30,-15,-9],
[4,73,81,-51],
[13,41,-91,54],
[49,47,-32,38],
[92,82,98,63],
[67,27,40,-39],
[49,52,-1,19],
[73,83,-62,-56],
[8,89,92,9],
[97,43,18,-81],
[59,30,60,78],
[4,43,-99,-81],
[36,78,47,-14],
[9,79,-52,-15],
[36,97,39,-83],
[62,15,-41,-47],
[50,2,-93,-4],
[95,10,-65,-87],
[63,69,68,71],
[0,96,65,80],
[44,74,29,46],
[100,77,-63,44],
[29,0,-46,53],
[58,63,-18,93],
[48,7,-55,-85],
[2,71,-45,-29],
[67,26,97,73],
[76,50,84,-31],
[68,0,-18,-58],
[4,45,57,38],
[23,94,-30,31],
[41,26,-68,5],
[36,52,-55,-3],
[33,96,7,96],
[61,25,99,-1],
[86,52,-68,23],
[7,43,59,-71],
[52,17,53,75],
[27,84,-75,-16],
[12,54,64,89],
[5,37,88,89],
[8,51,84,-82],
[97,30,-4,84],
[68,8,-81,78],
[93,79,-36,88],
[55,14,-17,58],
[9,65,-29,22],
[4,81,97,-21],
[99,81,89,55],
[18,30,-61,-60],
[34,102,-15,-88],
[96,42,-20,-76],
[25,46,-79,41],
[56,44,-96,56],
[66,8,-57,72],
[36,87,79,82],
[64,9,59,-23],
[1,49,21,6],
[38,59,-40,-73],
[41,102,5,94],
[19,87,57,-83],
[30,75,24,85],
[67,83,-18,58],
[68,3,61,-7],
[43,49,48,-16],
[8,99,-67,-46],
[42,58,68,-62],
[70,35,-10,24],
[34,32,39,29],
[19,9,46,78],
[1,52,4,-49],
[41,57,77,82],
[32,97,-70,-26],
[57,19,61,75],
[83,15,97,5],
[1,9,87,65],
[18,31,-61,51],
[76,27,85,-29],
[68,29,21,21],
[93,29,89,-30],
[37,10,70,34],
[75,1,-96,47],
[92,40,-42,-35],
[21,20,-61,2],
[84,13,16,42],
[60,16,68,78],
[33,87,94,34],
[53,51,45,27],
[24,90,71,91],
[30,36,68,-46],
[59,32,68,-79],
[95,54,-90,25],
[51,71,-40,14],
[57,100,-40,-82],
[31,4,-69,80],
[84,38,8,41],
[76,5,-49,15],
[29,95,-7,23],
[31,48,46,41],
[71,46,-41,27],
[8,54,-25,-37],
[12,65,-22,-65],
[28,54,29,-27],
[81,26,-76,-75],
[62,4,-17,15],
[54,12,-96,56],
[80,42,-96,17],
[3,44,-13,11],
[35,95,69,26],
[30,32,-8,29],
[76,85,49,-84],
[77,102,67,-88],
[1,37,-28,-32],
[25,88,-77,39],
[83,3,-58,-20],
[96,10,-20,45],
[61,7,94,6],
[84,100,-58,-23],
[26,76,55,-33],
[20,91,-37,20],
[51,22,14,-93],
[53,84,23,90],
[75,68,-25,82],
[45,57,-25,46],
[20,97,-43,26],
[11,70,58,-57],
[11,5,-27,57],
[25,81,9,-56],
[94,87,42,34],
[18,3,-45,18],
[59,87,84,12],
[88,64,75,11],
[12,83,-9,-68],
[33,52,72,-71],
[29,17,-38,53],
[95,5,19,-64],
[32,32,-11,-9],
[87,96,-62,59],
[57,3,-78,-91],
[60,88,29,39],
[98,27,43,-66],
[74,8,-77,-14],
[72,98,91,-72],
[63,64,26,1],
[11,49,-90,11],
[24,72,-22,44],
[69,72,-51,-25],
[61,12,52,26],
[24,82,-70,71],
[42,16,-93,27],
[63,95,45,93],
[10,88,64,20],
[90,32,-89,13],
[69,81,44,-57],
[33,37,-94,-52],
[27,50,49,53],
[80,32,28,13],
[76,68,5,-86],
[23,51,-21,-19],
[22,41,-14,73],
[73,54,91,68],
[22,46,94,89],
[7,73,95,71],
[12,63,-37,14],
[33,12,-56,-13],
[88,67,89,79],
[29,44,-85,-27],
[63,16,91,18],
[25,49,-24,33],
[70,76,-93,45],
[66,47,-96,-49],
[52,33,-73,7],
[5,39,10,84],
[89,65,58,-16],
[5,16,-91,34],
[27,95,16,12],
[27,0,-12,23],
[4,20,-6,-3],
[91,25,20,17],
[51,99,46,12],
[34,73,-15,55],
[100,23,-82,-49],
[19,80,-53,96],
[80,5,-19,64],
[50,100,-16,-80],
[69,44,22,51],
[26,76,91,93],
[48,88,-24,55],
[68,26,86,-23],
[49,95,31,-64],
[53,16,93,24],
[0,45,-36,-8],
[19,71,-78,-8],
[62,98,42,7],
[22,85,-99,6],
[48,48,-54,57],
[81,34,81,32],
[23,73,71,-89],
[57,64,-4,-47],
[89,17,26,-60],
[49,60,-45,47],
[25,16,13,-46],
[16,62,17,49],
[70,83,44,47],
[52,3,69,56],
[53,3,7,-58],
[59,28,-24,-4],
[39,67,15,14],
[59,71,-79,52],
[81,90,-58,47],
[87,6,51,50],
[32,58,16,-5],
[99,3,12,53],
[71,13,-49,86],
[93,25,4,59],
[54,95,25,12],
[66,76,45,-2],
[78,102,-42,61],
[47,54,37,-35],
[91,76,74,-2],
[98,87,73,-72],
[62,99,-3,-58],
[44,85,-24,-45],
[2,57,-13,-16],
[10,19,10,-1],
[87,40,51,73],
[77,27,67,81],
[35,96,-32,61],
[84,83,52,-2],
[10,63,18,14],
[57,101,-17,26],
[100,54,-59,-24],
[96,44,-20,54],
[11,83,2,17],
[17,87,9,-35],
[35,54,-93,76],
[66,2,36,23],
[37,59,-31,-81],
[67,36,-2,27],
[100,34,40,-86],
[52,81,-94,-2],
[30,10,39,45],
[7,74,80,90],
[77,79,59,28],
[40,33,96,98],
[60,70,99,-59],
[25,66,48,33],
[87,50,97,-19],
[86,6,35,99],
[16,51,95,60],
[72,31,84,27],
[99,18,-28,-55],
[95,38,-51,84],
[71,40,-19,-25],
[63,2,-2,37],
[65,80,68,-48],
[58,82,-40,3],
[79,27,59,-36],
[8,60,44,-30],
[53,49,-40,27],
[64,81,38,-91],
[62,55,-87,-8],
[46,43,7,-11],
[69,37,-65,-79],
[31,58,46,30],
[61,17,-87,37],
[56,98,-8,-15],
[62,102,-24,-9],
[46,28,54,59],
[21,29,-69,-71],
[83,65,67,38],
[94,52,59,65],
[32,47,31,5],
[95,34,-80,-3],
[32,37,-54,-76],
[89,18,82,-66],
[28,88,-47,9],
[62,20,98,-90],
[83,83,51,36],
[21,89,-83,-7],
[58,31,84,-79],
[75,49,-11,92],
[44,91,-39,12],
[17,82,-93,-80],
[94,56,-54,-82],
[56,43,69,-42],
[9,27,50,-41],
[6,49,-29,76],
[64,39,3,52],
[8,73,26,6],
[58,83,-64,-64],
[26,94,-8,-15],
[20,69,88,9],
[7,82,-12,96],
[33,25,-89,68],
[61,40,-34,78],
[25,39,56,49],
[60,18,-20,-42],
[91,30,-65,56],
[80,21,44,-77],
[74,102,67,-31],
[98,76,-53,76],
[21,23,87,29],
[75,57,-25,-78],
[13,11,-6,86],
[84,1,-19,-61],
[80,23,28,-93],
[24,17,-92,-85],
[4,55,70,61],
[33,84,-29,-69],
[30,82,24,-61],
[58,30,6,-52],
[1,71,3,-51],
[53,69,-64,-24],
[68,11,61,2],
[22,8,-69,-12],
[82,93,75,1],
[45,21,44,-89],
[11,81,-75,-29],
[59,77,42,44],
[11,34,10,-3],
[25,98,-69,58],
[37,55,30,-51],
[0,69,55,-90],
[89,12,35,82],
[58,65,-25,-24],
[29,102,-69,64],
[36,0,12,17],
[94,54,70,-56],
[48,95,90,-68],
[39,59,-94,34],
[23,18,-21,21],
[84,91,-81,-34],
[70,44,21,-95],
[87,73,4,-16],
[66,40,86,35],
[97,30,-82,-90],
[86,46,-61,33],
[90,73,-65,82],
[49,24,7,-44],
[76,77,-74,17],
[89,3,-56,-59],
[57,99,-25,42],
[72,101,-65,94],
[77,27,-81,-14],
[18,57,-84,-70],
[18,68,95,95],
[47,4,-70,-23],
[52,66,91,9],
[40,89,-23,-21],
[80,19,85,49],
[75,8,5,56],
[35,20,-32,-58],
[47,100,35,-63],
[66,60,60,25],
[36,81,-7,-86],
[17,55,-22,-84],
[47,94,54,4],
[75,22,-59,-96],
[0,48,-51,73],
[22,2,-22,64],
[100,29,53,59],
[68,5,-10,-31],
[42,3,-54,-17],
[97,31,58,-1],
[79,34,99,-30],
[95,66,-19,57],
[40,52,47,46],
[36,102,-67,-58],
[75,98,-4,39],
[89,90,-52,50],
[54,50,30,57],
[20,72,-84,-32],
]
const w = 101;
const h = 103;
const midY = Math.floor(h / 2);
const midX = Math.floor(w / 2);
const getQuadrant = (px, py, map) => {
return (midY === py || midX === px) ? '' : `${midY < py}_${midX < px}`;
}
const calculateNewPos = (p, v, len, seconds = 100) => (len + (p + v * seconds) % len) % len;
// part 1
const res = Object.values(map.reduce((acc, [px, py, vx, vy]) => {
const quad = getQuadrant(calculateNewPos(px, vx, w), calculateNewPos(py, vy, h));
if (quad) { acc[quad] = (acc[quad] || 0) + 1; }
return acc;
}, {})).reduce((acc, num) => acc * num, 1);
// part 2
for (let seconds = 0; seconds < 10000; ++seconds) {
const newMap = new Array(h).fill(null).map(() => new Array(w).fill(0));
map.forEach(([px, py, vx, vy]) => {
newMap[calculateNewPos(py, vy, h, seconds)][calculateNewPos(px, vx, w, seconds)] += 1;
});
if (newMap.every(row => row.every(x => x === 0 || x === 1))) {
const picture = newMap.map(arr => arr.map(x => x === 0 ? ' ' : '*').join("")).join("\n");
console.log(seconds);
console.table(picture);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment