git

My personal website source code
Log | Files | Refs | Submodules | README | LICENSE

commit 63bcc7603d6b31bd55b232f137e0e24c87aa2eeb
parent 82ebf9678c26287d141b8e57b558d33a3cda294b
Author: Luís Ferreira <contact@lsferreira.net>
Date:   Fri, 17 Dec 2021 23:23:34 +0000

posts: Add 'Solution in D for Advent of Code 2021, Day 6'

Signed-off-by: Luís Ferreira <contact@lsferreira.net>

Diffstat:
Acontent/posts/zet-7-aoc-2021-06.md | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 74 insertions(+), 0 deletions(-)

diff --git a/content/posts/zet-7-aoc-2021-06.md b/content/posts/zet-7-aoc-2021-06.md @@ -0,0 +1,74 @@ +--- +title: 'Zettelkasten #7: Solution in D for Advent of Code 2021, Day 6' +date: '2021-12-17T22:19:00+01:00' +tags: ['zettelkasten', 'zet', 'dlang', 'aoc', 'aoc2021', 'adventofcode'] +description: "This post describes my solution in the D programming language for +the 6th puzzle of the Advent of Code 2021." +--- + +## The challenge + +### Part 1 + +> The sea floor is getting steeper. Maybe the sleigh keys got carried this way? +> +> A massive school of glowing lanternfish swims past. They must spawn quickly +> to reach such large numbers - maybe exponentially quickly? You should model +> their growth rate to be sure. +> +> Although you know nothing about this specific species of lanternfish, you +> make some guesses about their attributes. Surely, each lanternfish creates a +> new lanternfish once every 7 days. +> +> However, this process isn't necessarily synchronized between every +> lanternfish - one lanternfish might have 2 days left until it creates another +> lanternfish, while another might have 4. So, you can model each fish as a +> single number that represents the number of days until it creates a new +> lanternfish. +> +> Furthermore, you reason, a new lanternfish would surely need slightly longer +> before it's capable of producing more lanternfish: two more days for its +> first cycle. +> +> So, suppose you have a lanternfish with an internal timer value of 3: +> +> - After one day, its internal timer would become 2. +> - After another day, its internal timer would become 1. +> - After another day, its internal timer would become 0. +> - After another day, its internal timer would reset to 6, and it would create +> a new lanternfish with an internal timer of 8. +> - After another day, the first lanternfish would have an internal timer of 5, +> and the second lanternfish would have an internal timer of 7. +> +> A lanternfish that creates a new fish resets its timer to 6, not 7 (because 0 +> is included as a valid timer value). The new lanternfish starts with an +> internal timer of 8 and does not start counting down until the next day. +> +> Realizing what you're trying to do, the submarine automatically produces a +> list of the ages of several hundred nearby lanternfish (your puzzle input). +> +> [...] +> +> Find a way to simulate lanternfish. How many lanternfish would there be after +> 80 days? + +You can read the challenge more in depth, +[here](https://adventofcode.com/2021/day/6). + +### Part 2 + +> Suppose the lanternfish live forever and have unlimited food and space. Would +> they take over the entire ocean? +> +> [...] +> +> How many lanternfish would there be after 256 days? + +## Full solution + +```d +auto input = stdin.byLineCopy().front.splitter(",").map!(to!ubyte).array; // input +auto count = iota(0,9).map!(s => input.count!(a => a == s)).array; // count fish lifes +iota(0,256).each!((d){count[(d+7)%9]+=count[d%9];}); // calculate number of fishes per day +count.sum.writeln; // sum all fishes +```