git

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

zet-7-aoc-2021-06.md (2997B)


      1 ---
      2 title: 'Zettelkasten #7: Solution in D for Advent of Code 2021, Day 6'
      3 date: '2021-12-17T22:19:00+01:00'
      4 tags: ['zettelkasten', 'zet', 'dlang', 'aoc', 'aoc2021', 'adventofcode']
      5 description: "This post describes my solution in the D programming language for
      6 the 6th puzzle of the Advent of Code 2021."
      7 ---
      8 
      9 ## The challenge
     10 
     11 ### Part 1
     12 
     13 > The sea floor is getting steeper. Maybe the sleigh keys got carried this way?
     14 >
     15 > A massive school of glowing lanternfish swims past. They must spawn quickly
     16 > to reach such large numbers - maybe exponentially quickly? You should model
     17 > their growth rate to be sure.
     18 >
     19 > Although you know nothing about this specific species of lanternfish, you
     20 > make some guesses about their attributes. Surely, each lanternfish creates a
     21 > new lanternfish once every 7 days.
     22 >
     23 > However, this process isn't necessarily synchronized between every
     24 > lanternfish - one lanternfish might have 2 days left until it creates another
     25 > lanternfish, while another might have 4. So, you can model each fish as a
     26 > single number that represents the number of days until it creates a new
     27 > lanternfish.
     28 >
     29 > Furthermore, you reason, a new lanternfish would surely need slightly longer
     30 > before it's capable of producing more lanternfish: two more days for its
     31 > first cycle.
     32 >
     33 > So, suppose you have a lanternfish with an internal timer value of 3:
     34 >
     35 > - After one day, its internal timer would become 2.
     36 > - After another day, its internal timer would become 1.
     37 > - After another day, its internal timer would become 0.
     38 > - After another day, its internal timer would reset to 6, and it would create
     39 >   a new lanternfish with an internal timer of 8.
     40 > - After another day, the first lanternfish would have an internal timer of 5,
     41 >   and the second lanternfish would have an internal timer of 7.
     42 >
     43 > A lanternfish that creates a new fish resets its timer to 6, not 7 (because 0
     44 > is included as a valid timer value). The new lanternfish starts with an
     45 > internal timer of 8 and does not start counting down until the next day.
     46 >
     47 > Realizing what you're trying to do, the submarine automatically produces a
     48 > list of the ages of several hundred nearby lanternfish (your puzzle input).
     49 >
     50 > [...]
     51 >
     52 > Find a way to simulate lanternfish. How many lanternfish would there be after
     53 > 80 days?
     54 
     55 You can read the challenge more in depth,
     56 [here](https://adventofcode.com/2021/day/6).
     57 
     58 ### Part 2
     59 
     60 > Suppose the lanternfish live forever and have unlimited food and space. Would
     61 > they take over the entire ocean?
     62 >
     63 > [...]
     64 >
     65 > How many lanternfish would there be after 256 days?
     66 
     67 ## Full solution
     68 
     69 ```d
     70 auto input = stdin.byLineCopy().front.splitter(",").map!(to!ubyte).array; // input
     71 auto count = iota(0,9).map!(s => input.count!(a => a == s)).array;        // count fish lifes
     72 iota(0,256).each!((d){count[(d+7)%9]+=count[d%9];});                      // calculate number of fishes per day
     73 count.sum.writeln;                                                        // sum all fishes
     74 ```