Files
AoC/2024/14/index.ts
2024-12-14 08:26:36 +00:00

59 lines
1.3 KiB
TypeScript

const fs = require('fs');
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
interface Coord {
x: number,
y: number,
}
interface Bot {
pos: Coord,
vel: Coord,
}
const bots: Array<Bot> = [];
input.split("\n").forEach((line: string) => {
const matches = line.match(/p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)/);
if (!matches) {
return;
}
bots.push({
pos: { x: parseInt(matches[1]), y: parseInt(matches[2]) },
vel: { x: parseInt(matches[3]), y: parseInt(matches[4]) },
});
});
const width = 101;
const height = 103;
const seconds = 100;
const quads = [0, 0, 0, 0];
bots.forEach((bot) => {
let newX = (bot.pos.x + (bot.vel.x * seconds)) % width;
if (newX < 0) {
newX = width + newX;
}
bot.pos.x = newX;
let newY = (bot.pos.y + (bot.vel.y * seconds)) % height;
if (newY < 0) {
newY = height + newY;
}
bot.pos.y = newY;
if (newX < (width - 1) / 2 && newY < (height - 1) / 2) {
quads[0]++;
} else if (newX > (width - 1) / 2 && newY < (height - 1) / 2) {
quads[1]++;
} else if (newX < (width - 1) / 2 && newY > (height - 1) / 2) {
quads[2]++;
} else if (newX > (width - 1) / 2 && newY > (height - 1) / 2) {
quads[3]++;
}
});
console.log(quads);
console.log(quads.reduce((c, n) => c * n, 1));