day6
This commit is contained in:
1
src/day6/example.txt
Normal file
1
src/day6/example.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
3,4,3,1,2
|
||||||
35
src/day6/index.ts
Normal file
35
src/day6/index.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
import path from 'path';
|
||||||
|
import {readFile} from "../utils";
|
||||||
|
|
||||||
|
let input = readFile(path.resolve(__dirname, 'input.txt'))[0].split(",");
|
||||||
|
|
||||||
|
// prepare the buckets of fish
|
||||||
|
let buckets = [0, 0, 0, 0, 0, 0, 0, 0, 0];
|
||||||
|
input.forEach(number => {
|
||||||
|
const num = parseInt(number);
|
||||||
|
buckets[num - 1]++;
|
||||||
|
})
|
||||||
|
|
||||||
|
const numFish = (days) => {
|
||||||
|
for (let k = 1; k < days; k++) {
|
||||||
|
let diff = [0, 0, 0, 0, 0, 0, 0, 0, 0];
|
||||||
|
for (let i = 0; i < 9; i++) {
|
||||||
|
const numFish = buckets[i];
|
||||||
|
if (i === 0) {
|
||||||
|
diff[8] += numFish;
|
||||||
|
diff[6] += numFish;
|
||||||
|
diff[0] -= numFish;
|
||||||
|
} else {
|
||||||
|
diff[i] -= numFish;
|
||||||
|
diff[i - 1] += numFish;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let i = 8; i >= 0; i--) {
|
||||||
|
buckets[i] += diff[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buckets.reduce((a, b) => a + b, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Part 1:", numFish(80));
|
||||||
|
console.log("Part 2:", numFish(256));
|
||||||
1
src/day6/input.txt
Normal file
1
src/day6/input.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
3,5,4,1,2,1,5,5,1,1,1,1,4,1,4,5,4,5,1,3,1,1,1,4,1,1,3,1,1,5,3,1,1,3,1,3,1,1,1,4,1,2,5,3,1,4,2,3,1,1,2,1,1,1,4,1,1,1,1,2,1,1,1,3,1,1,4,1,4,1,5,1,4,2,1,1,5,4,4,4,1,4,1,1,1,1,3,1,5,1,4,5,3,1,4,1,5,2,2,5,1,3,2,2,5,4,2,3,4,1,2,1,1,2,1,1,5,4,1,1,1,1,3,1,5,4,1,5,1,1,4,3,4,3,1,5,1,1,2,1,1,5,3,1,1,1,1,1,5,1,1,1,1,1,1,1,2,2,5,5,1,2,1,2,1,1,5,1,3,1,5,2,1,4,1,5,3,1,1,1,2,1,3,1,4,4,1,1,5,1,1,4,1,4,2,3,5,2,5,1,3,1,2,1,4,1,1,1,1,2,1,4,1,3,4,1,1,1,1,1,1,1,2,1,5,1,1,1,1,2,3,1,1,2,3,1,1,3,1,1,3,1,3,1,3,3,1,1,2,1,3,2,3,1,1,3,5,1,1,5,5,1,2,1,2,2,1,1,1,5,3,1,1,3,5,1,3,1,5,3,4,2,3,2,1,3,1,1,3,4,2,1,1,3,1,1,1,1,1,1
|
||||||
Reference in New Issue
Block a user