aoc-2021-rust

Advent of Code 2021 Solutions in Rust
git clone https://git.sinitax.com/sinitax/aoc-2021-rust
Log | Files | Refs | README | sfeed.txt

part1 (3390B)


      1--- Day 6: Lanternfish ---
      2
      3The sea floor is getting steeper. Maybe the sleigh keys got carried this way?
      4
      5A massive school of glowing lanternfish swims past. They must spawn quickly to reach such large
      6numbers - maybe exponentially quickly? You should model their growth rate to be sure.
      7
      8Although you know nothing about this specific species of lanternfish, you make some guesses about
      9their attributes. Surely, each lanternfish creates a new lanternfish once every 7 days.
     10
     11However, this process isn't necessarily synchronized between every lanternfish - one lanternfish
     12might have 2 days left until it creates another lanternfish, while another might have 4. So, you can
     13model each fish as a single number that represents the number of days until it creates a new
     14lanternfish.
     15
     16Furthermore, you reason, a new lanternfish would surely need slightly longer before it's capable of
     17producing more lanternfish: two more days for its first cycle.
     18
     19So, suppose you have a lanternfish with an internal timer value of 3:
     20
     21
     22 - After one day, its internal timer would become 2.
     23
     24 - After another day, its internal timer would become 1.
     25
     26 - After another day, its internal timer would become 0.
     27
     28 - After another day, its internal timer would reset to 6, and it would create a new lanternfish
     29with an internal timer of 8.
     30
     31 - After another day, the first lanternfish would have an internal timer of 5, and the second
     32lanternfish would have an internal timer of 7.
     33
     34
     35A lanternfish that creates a new fish resets its timer to 6, not 7 (because 0 is included as a valid
     36timer value). The new lanternfish starts with an internal timer of 8 and does not start counting
     37down until the next day.
     38
     39Realizing what you're trying to do, the submarine automatically produces a list of the ages of
     40several hundred nearby lanternfish (your puzzle input). For example, suppose you were given the
     41following list:
     42
     433,4,3,1,2
     44This list means that the first fish has an internal timer of 3, the second fish has an internal
     45timer of 4, and so on until the fifth fish, which has an internal timer of 2. Simulating these fish
     46over several days would proceed as follows:
     47
     48Initial state: 3,4,3,1,2
     49After  1 day:  2,3,2,0,1
     50After  2 days: 1,2,1,6,0,8
     51After  3 days: 0,1,0,5,6,7,8
     52After  4 days: 6,0,6,4,5,6,7,8,8
     53After  5 days: 5,6,5,3,4,5,6,7,7,8
     54After  6 days: 4,5,4,2,3,4,5,6,6,7
     55After  7 days: 3,4,3,1,2,3,4,5,5,6
     56After  8 days: 2,3,2,0,1,2,3,4,4,5
     57After  9 days: 1,2,1,6,0,1,2,3,3,4,8
     58After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8
     59After 11 days: 6,0,6,4,5,6,0,1,1,2,6,7,8,8,8
     60After 12 days: 5,6,5,3,4,5,6,0,0,1,5,6,7,7,7,8,8
     61After 13 days: 4,5,4,2,3,4,5,6,6,0,4,5,6,6,6,7,7,8,8
     62After 14 days: 3,4,3,1,2,3,4,5,5,6,3,4,5,5,5,6,6,7,7,8
     63After 15 days: 2,3,2,0,1,2,3,4,4,5,2,3,4,4,4,5,5,6,6,7
     64After 16 days: 1,2,1,6,0,1,2,3,3,4,1,2,3,3,3,4,4,5,5,6,8
     65After 17 days: 0,1,0,5,6,0,1,2,2,3,0,1,2,2,2,3,3,4,4,5,7,8
     66After 18 days: 6,0,6,4,5,6,0,1,1,2,6,0,1,1,1,2,2,3,3,4,6,7,8,8,8,8
     67
     68Each day, a 0 becomes a 6 and adds a new 8 to the end of the list, while each other number decreases
     69by 1 if it was present at the start of the day.
     70
     71In this example, after 18 days, there are a total of 26 fish. After 80 days, there would be a total
     72of 5934.
     73
     74Find a way to simulate lanternfish. How many lanternfish would there be after 80 days?
     75
     76