From 7160b4fa784d47159227360cce89c2ec8ce99ea3 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 6 Dec 2021 11:25:43 -0500 Subject: [PATCH] 2021-06 --- 2021/06/part1.mjs | 22 ++++++++++++++++++++++ 2021/06/test.mjs | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 2021/06/part1.mjs create mode 100644 2021/06/test.mjs diff --git a/2021/06/part1.mjs b/2021/06/part1.mjs new file mode 100644 index 0000000..8cb10ec --- /dev/null +++ b/2021/06/part1.mjs @@ -0,0 +1,22 @@ +import fs from "fs-extra"; +import fp from "lodash/fp.js"; +import _ from "lodash"; + +export const processInput = (input) => + input.split(',').map( x => parseInt(x) ); + +export function solution(input,days = 80) { + let fish = Array.from({length:8}, ()=>0); + input.forEach( x => fish[x]++ ); + + for( let i = 0; i< days; i++ ) { + const breeders = fish.shift(); + if(breeders ) { + fish[8] = breeders; + fish[6] = (fish[6] || 0) + breeders; + } + // console.log(fish); + } + + return _.sum(fish); +}; diff --git a/2021/06/test.mjs b/2021/06/test.mjs new file mode 100644 index 0000000..120b721 --- /dev/null +++ b/2021/06/test.mjs @@ -0,0 +1,18 @@ +import tap from "tap"; +import fs from "fs-extra"; + +import * as p1 from "./part1.mjs"; + +const sample = p1.processInput("3,4,3,1,2"); +const input = fs.readFile("input", "utf8").then(p1.processInput); + +tap.test("part1", async (t) => { + t.equal(p1.solution(await sample, 18), 26); + t.equal(p1.solution(await sample), 5934); + t.equal(p1.solution(await input), 358214); +}); + +tap.test("part2", async (t) => { + t.equal(p1.solution(await sample, 256), 26984457539); + t.equal(p1.solution(await input, 256), 1622533344325); +});