From 950e552b952719489f5a5e28a8703cb65eb6b87b Mon Sep 17 00:00:00 2001 From: Nathan van Ofwegen Date: Tue, 7 Dec 2021 10:02:57 +0100 Subject: [PATCH] day6 --- src/day6/example.txt | 1 + src/day6/index.ts | 35 +++++++++++++++++++++++++++++++++++ src/day6/input.txt | 1 + 3 files changed, 37 insertions(+) create mode 100644 src/day6/example.txt create mode 100644 src/day6/index.ts create mode 100644 src/day6/input.txt diff --git a/src/day6/example.txt b/src/day6/example.txt new file mode 100644 index 0000000..a7af2b1 --- /dev/null +++ b/src/day6/example.txt @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file diff --git a/src/day6/index.ts b/src/day6/index.ts new file mode 100644 index 0000000..5e74480 --- /dev/null +++ b/src/day6/index.ts @@ -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)); \ No newline at end of file diff --git a/src/day6/input.txt b/src/day6/input.txt new file mode 100644 index 0000000..07d6764 --- /dev/null +++ b/src/day6/input.txt @@ -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 \ No newline at end of file