From ccf47f1cc4286bbf72adc84f67fff00fb7c7f4e3 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 14 Dec 2024 08:26:36 +0000 Subject: [PATCH] Day 14 --- 2024/14/index.ts | 58 ++++++++++++++++++++++++++++++++++ 2024/14/index2.ts | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 2024/14/index.ts create mode 100644 2024/14/index2.ts diff --git a/2024/14/index.ts b/2024/14/index.ts new file mode 100644 index 0000000..f528fd4 --- /dev/null +++ b/2024/14/index.ts @@ -0,0 +1,58 @@ +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 = []; + +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)); diff --git a/2024/14/index2.ts b/2024/14/index2.ts new file mode 100644 index 0000000..e8c1000 --- /dev/null +++ b/2024/14/index2.ts @@ -0,0 +1,80 @@ +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 = []; + +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; + +let seconds = 0; + +function printBots() { + const botRows: Array> = []; + for (let y = 0; y < height; y++) { + botRows[y] = []; + for (let x = 0; x < width; x++) { + botRows[y][x] = ' '; + } + } + + bots.forEach((bot) => { + botRows[bot.pos.y][bot.pos.x] = '.'; + }); + + console.log(botRows.map((r) => r.join('')).join("\n")); + console.log(seconds); +} +printBots(); + +function sleep() { + const promise: Promise = new Promise((resolve) => { + setTimeout(() => resolve(), 100); + }); + return promise; +} + +async function go() { + while (true) { + seconds++; + bots.forEach((bot) => { + let newX = (bot.pos.x + (bot.vel.x)) % width; + if (newX < 0) { + newX = width + newX; + } + bot.pos.x = newX; + let newY = (bot.pos.y + (bot.vel.y)) % height; + if (newY < 0) { + newY = height + newY; + } + bot.pos.y = newY; + }); + if ((seconds - 72) % 103 === 0) { + printBots(); + await sleep(); + } + } +} + +go(); \ No newline at end of file