2021-06
This commit is contained in:
parent
8011c14aeb
commit
7160b4fa78
22
2021/06/part1.mjs
Normal file
22
2021/06/part1.mjs
Normal file
@ -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);
|
||||
};
|
18
2021/06/test.mjs
Normal file
18
2021/06/test.mjs
Normal file
@ -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);
|
||||
});
|
Loading…
Reference in New Issue
Block a user