Skip to content

Commit

Permalink
Day 14
Browse files Browse the repository at this point in the history
  • Loading branch information
Av32000 committed Dec 14, 2024
1 parent c54beb8 commit 8ba8103
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 14/part1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { readFileSync } = require("fs");
// Parse Input
const inputFile = "input.txt";
const input = readFileSync(inputFile).toString().trim();
const robots = [];

const GRID_WIDTH = 101;
const GRID_HEIGHT = 103;

input.split("\n").forEach((line) => {
const regex = /-?\d+/g;
const values = line.match(regex).map((x) => parseInt(x));

robots.push({
id: robots.length,
position: [values[0], values[1]],
velocity: [values[2], values[3]],
});
});

function tick() {
robots.forEach((robot) => {
let x = (robot.position[0] + robot.velocity[0]) % GRID_WIDTH;
let y = (robot.position[1] + robot.velocity[1]) % GRID_HEIGHT;

if (x < 0) x = GRID_WIDTH + x;
if (y < 0) y = GRID_HEIGHT + y;

robot.position = [x, y];
});
}

for (let i = 0; i < 100; i++) {
tick();
}

let quadrants = [0, 0, 0, 0];
let halfWidth = Math.floor(GRID_WIDTH / 2);
let halfHeight = Math.floor(GRID_HEIGHT / 2);

robots.forEach((robot) => {
let [x, y] = robot.position;
if (x < halfWidth && y < halfHeight) quadrants[0]++;
if (x > halfWidth && y < halfHeight) quadrants[1]++;
if (x > halfWidth && y > halfHeight) quadrants[2]++;
if (x < halfWidth && y > halfHeight) quadrants[3]++;
});

let result = quadrants.reduce((acc, num) => acc * num, 1);
console.log(result);
75 changes: 75 additions & 0 deletions 14/part2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const { readFileSync } = require("fs");
const readline = require("readline");

// Parse Input
const inputFile = "input.txt";
const input = readFileSync(inputFile).toString().trim();
const robots = [];

const GRID_WIDTH = 101;
const GRID_HEIGHT = 103;

input.split("\n").forEach((line) => {
const regex = /-?\d+/g;
const values = line.match(regex).map((x) => parseInt(x));

robots.push({
id: robots.length,
position: [values[0], values[1]],
velocity: [values[2], values[3]],
});
});

function displayGrid() {
const grid = Array.from({ length: GRID_HEIGHT }, () =>
Array(GRID_WIDTH).fill(0)
);

robots.forEach((robot) => {
grid[robot.position[1]][robot.position[0]]++;
});

grid.forEach((line) => {
console.log(line.map((x) => (x == 0 ? "." : x.toString())).join(""));
});
console.log("Elapsed Seconds", elapsedSeconds);
}

function computeVariations() {
let totalDistance = 0;

let coords = robots.map((r) => r.position);

for (let i = 1; i < coords.length; i++) {
const x1 = coords[i - 1][0];
const y1 = coords[i - 1][1];
const x2 = coords[i][0];
const y2 = coords[i][1];

totalDistance += Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}

return totalDistance / (coords.length - 1);
}

function tick() {
robots.forEach((robot) => {
let x = (robot.position[0] + robot.velocity[0]) % GRID_WIDTH;
let y = (robot.position[1] + robot.velocity[1]) % GRID_HEIGHT;

if (x < 0) x = GRID_WIDTH + x;
if (y < 0) y = GRID_HEIGHT + y;

robot.position = [x, y];
});
}

let elapsedSeconds = 0;
while (true) {
tick();
elapsedSeconds++;
if (computeVariations() < 40) {
displayGrid();
break;
}
}

0 comments on commit 8ba8103

Please sign in to comment.