Solutions for day six
This commit is contained in:
parent
d306294bda
commit
a85f4063d4
20
06/code.js
Normal file
20
06/code.js
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
const fs = require('fs');
|
||||||
|
const inputArray = fs.readFileSync('input.txt').toString().split(",").map(Number);
|
||||||
|
|
||||||
|
function countFish(days) {
|
||||||
|
const fishPerDay = new Array(9).fill(0); // Array of number of fish at each day remaining
|
||||||
|
inputArray.forEach(f => (fishPerDay[f]++)); // Populate the array based on the input
|
||||||
|
|
||||||
|
for (let d = 0; d < days; d++){
|
||||||
|
const fish = fishPerDay.shift(); // The number of fish at zero
|
||||||
|
(fishPerDay.push(fish)) && (fishPerDay[6] += fish); // Put that many fish at eight and add this number to six
|
||||||
|
}
|
||||||
|
|
||||||
|
return fishPerDay.reduce((a, b) => a + b, 0); // add up all the fish
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part One
|
||||||
|
console.log(countFish(80));
|
||||||
|
|
||||||
|
// Part Two
|
||||||
|
console.log(countFish(256));
|
1
06/input.txt
Normal file
1
06/input.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
4,5,3,2,3,3,2,4,2,1,2,4,5,2,2,2,4,1,1,1,5,1,1,2,5,2,1,1,4,4,5,5,1,2,1,1,5,3,5,2,4,3,2,4,5,3,2,1,4,1,3,1,2,4,1,1,4,1,4,2,5,1,4,3,5,2,4,5,4,2,2,5,1,1,2,4,1,4,4,1,1,3,1,2,3,2,5,5,1,1,5,2,4,2,2,4,1,1,1,4,2,2,3,1,2,4,5,4,5,4,2,3,1,4,1,3,1,2,3,3,2,4,3,3,3,1,4,2,3,4,2,1,5,4,2,4,4,3,2,1,5,3,1,4,1,1,5,4,2,4,2,2,4,4,4,1,4,2,4,1,1,3,5,1,5,5,1,3,2,2,3,5,3,1,1,4,4,1,3,3,3,5,1,1,2,5,5,5,2,4,1,5,1,2,1,1,1,4,3,1,5,2,3,1,3,1,4,1,3,5,4,5,1,3,4,2,1,5,1,3,4,5,5,2,1,2,1,1,1,4,3,1,4,2,3,1,3,5,1,4,5,3,1,3,3,2,2,1,5,5,4,3,2,1,5,1,3,1,3,5,1,1,2,1,1,1,5,2,1,1,3,2,1,5,5,5,1,1,5,1,4,1,5,4,2,4,5,2,4,3,2,5,4,1,1,2,4,3,2,1
|
Loading…
Reference in New Issue
Block a user