From ee8390432a691e558e785991811c1fcd776b510f Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sat, 11 Dec 2021 12:08:28 -0500 Subject: [PATCH] 2021-11 --- 2021/11/part1.mjs | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 2021/11/part2.mjs | 19 +++++++++++++ 2021/11/sample | 10 +++++++ 2021/11/test.mjs | 23 ++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 2021/11/part1.mjs create mode 100644 2021/11/part2.mjs create mode 100644 2021/11/sample create mode 100644 2021/11/test.mjs diff --git a/2021/11/part1.mjs b/2021/11/part1.mjs new file mode 100644 index 0000000..57b2926 --- /dev/null +++ b/2021/11/part1.mjs @@ -0,0 +1,68 @@ +import fs from "fs-extra"; +import fp from "lodash/fp.js"; +import _ from "lodash"; + +export const processInput = (file) => + fs.readFile(file, "utf8").then((content) => + content + .split("\n") + .filter((x) => x) + .map((line) => line.split("").map((x) => parseInt(x))) + ); + +export const genNeighbours = (maxX, maxY) => (x, y) => + [ + [-1, 0], + [1, 0], + [0, 1], + [0, -1], + [1, 1], + [-1, -1], + [1, -1], + [-1, 1], + ] + .map((delta) => _.zip(delta, [x, y]).map(_.sum)) + .filter(([x, y]) => { + if (x < 0 || y < 0) return false; + if (x >= maxX || y >= maxY) return false; + return true; + }); + +function powerUp(x, y, grid, toClear) { + grid[x][y]++; + + if (grid[x][y] !== 10) return 0; + + // FLASH! + toClear.push([x, y]); + + return _.sum([ + 1, + ...genNeighbours(grid.length, grid[0].length)(x, y).map((coords) => + powerUp(...coords, grid, toClear) + ), + ]); +} + +export function playTurn(grid) { + let flashes = 0; + let toClear = []; + + for (const x in grid) { + for (const y in grid[x]) { + flashes += powerUp(parseInt(x), parseInt(y), grid, toClear); + } + } + + for (const [x, y] of toClear) { + grid[x][y] = 0; + } + + return flashes; +} + +export const printGrid = (grid) => + grid.forEach((line) => console.log(line.join(""))); + +export const solution = (grid) => + _.sum(_.range(100).map(() => playTurn(grid))); diff --git a/2021/11/part2.mjs b/2021/11/part2.mjs new file mode 100644 index 0000000..a5329a9 --- /dev/null +++ b/2021/11/part2.mjs @@ -0,0 +1,19 @@ +import fs from "fs-extra"; +import fp from "lodash/fp.js"; +import _ from "lodash"; + +import * as p1 from './part1.mjs'; + +const allFlashed = grid => grid.every( + row => row.every( x => x === 0 ) + ); + +export function solution(grid) { + let step = 0; + + while(true) { + step++; + p1.playTurn(grid); + if( allFlashed(grid) ) return step; + } +} diff --git a/2021/11/sample b/2021/11/sample new file mode 100644 index 0000000..03743f6 --- /dev/null +++ b/2021/11/sample @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 diff --git a/2021/11/test.mjs b/2021/11/test.mjs new file mode 100644 index 0000000..dc33bdb --- /dev/null +++ b/2021/11/test.mjs @@ -0,0 +1,23 @@ +// https://adventofcode.com/2021/day/11 + +import tap from "tap"; +import fs from "fs-extra"; + +import * as p1 from "./part1.mjs"; +import * as p2 from "./part2.mjs"; + + +tap.test("part1", async (t) => { + const sample = p1.processInput('sample'); + const input = p1.processInput('input'); + + t.equal(p1.solution(await sample), 1656); + t.equal(p1.solution(await input), 1729); +}); + +tap.test("part2", async (t) => { + const sample = await p1.processInput('sample'); + const input = await p1.processInput('input'); + t.equal(p2.solution(sample), 195); + t.equal(p2.solution(input), 237); +});