From 27234dbb63236ba405be6496f6b8a7d60bbf7751 Mon Sep 17 00:00:00 2001 From: Alfred Melch Date: Wed, 5 Jan 2022 17:51:43 +0100 Subject: [PATCH] Solve 2021/06 --- 2021/day06/Cargo.lock | 7 ++++ 2021/day06/Cargo.toml | 8 ++++ 2021/day06/README.md | 84 +++++++++++++++++++++++++++++++++++++++ 2021/day06/input/example0 | 1 + 2021/day06/input/example1 | 19 +++++++++ 2021/day06/input/input | 1 + 2021/day06/src/main.rs | 66 ++++++++++++++++++++++++++++++ 7 files changed, 186 insertions(+) create mode 100644 2021/day06/Cargo.lock create mode 100644 2021/day06/Cargo.toml create mode 100644 2021/day06/README.md create mode 100644 2021/day06/input/example0 create mode 100644 2021/day06/input/example1 create mode 100644 2021/day06/input/input create mode 100644 2021/day06/src/main.rs diff --git a/2021/day06/Cargo.lock b/2021/day06/Cargo.lock new file mode 100644 index 0000000..0c65426 --- /dev/null +++ b/2021/day06/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day06" +version = "0.1.0" diff --git a/2021/day06/Cargo.toml b/2021/day06/Cargo.toml new file mode 100644 index 0000000..2f276f3 --- /dev/null +++ b/2021/day06/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day06" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2021/day06/README.md b/2021/day06/README.md new file mode 100644 index 0000000..69b9e5d --- /dev/null +++ b/2021/day06/README.md @@ -0,0 +1,84 @@ +https://adventofcode.com/2021/day/6 + +## \--- Day 6: Lanternfish --- + +The sea floor is getting steeper. Maybe the sleigh keys got carried this way? + +A massive school of glowing +[lanternfish](https://en.wikipedia.org/wiki/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). +For example, suppose you were given the following list: + +[code] + + 3,4,3,1,2 +[/code] + +This list means that the first fish has an internal timer of `3`, the second +fish has an internal timer of `4`, and so on until the fifth fish, which has +an internal timer of `2`. Simulating these fish over several days would +proceed as follows: + +[code] + + Initial state: 3,4,3,1,2 + After 1 day: 2,3,2,0,1 + After 2 days: 1,2,1,6,0,8 + After 3 days: 0,1,0,5,6,7,8 + After 4 days: 6,0,6,4,5,6,7,8,8 + After 5 days: 5,6,5,3,4,5,6,7,7,8 + After 6 days: 4,5,4,2,3,4,5,6,6,7 + After 7 days: 3,4,3,1,2,3,4,5,5,6 + After 8 days: 2,3,2,0,1,2,3,4,4,5 + After 9 days: 1,2,1,6,0,1,2,3,3,4,8 + After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8 + After 11 days: 6,0,6,4,5,6,0,1,1,2,6,7,8,8,8 + After 12 days: 5,6,5,3,4,5,6,0,0,1,5,6,7,7,7,8,8 + After 13 days: 4,5,4,2,3,4,5,6,6,0,4,5,6,6,6,7,7,8,8 + After 14 days: 3,4,3,1,2,3,4,5,5,6,3,4,5,5,5,6,6,7,7,8 + After 15 days: 2,3,2,0,1,2,3,4,4,5,2,3,4,4,4,5,5,6,6,7 + After 16 days: 1,2,1,6,0,1,2,3,3,4,1,2,3,3,3,4,4,5,5,6,8 + After 17 days: 0,1,0,5,6,0,1,2,2,3,0,1,2,2,2,3,3,4,4,5,7,8 + After 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 + +[/code] + +Each day, a `0` becomes a `6` and adds a new `8` to the end of the list, while +each other number decreases by 1 if it was present at the start of the day. + +In this example, after 18 days, there are a total of `26` fish. After 80 days, +there would be a total of `_5934_`. + +Find a way to simulate lanternfish. _How many lanternfish would there be after +80 days?_ + diff --git a/2021/day06/input/example0 b/2021/day06/input/example0 new file mode 100644 index 0000000..55129f1 --- /dev/null +++ b/2021/day06/input/example0 @@ -0,0 +1 @@ +3,4,3,1,2 diff --git a/2021/day06/input/example1 b/2021/day06/input/example1 new file mode 100644 index 0000000..09e953e --- /dev/null +++ b/2021/day06/input/example1 @@ -0,0 +1,19 @@ +Initial state: 3,4,3,1,2 +After 1 day: 2,3,2,0,1 +After 2 days: 1,2,1,6,0,8 +After 3 days: 0,1,0,5,6,7,8 +After 4 days: 6,0,6,4,5,6,7,8,8 +After 5 days: 5,6,5,3,4,5,6,7,7,8 +After 6 days: 4,5,4,2,3,4,5,6,6,7 +After 7 days: 3,4,3,1,2,3,4,5,5,6 +After 8 days: 2,3,2,0,1,2,3,4,4,5 +After 9 days: 1,2,1,6,0,1,2,3,3,4,8 +After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8 +After 11 days: 6,0,6,4,5,6,0,1,1,2,6,7,8,8,8 +After 12 days: 5,6,5,3,4,5,6,0,0,1,5,6,7,7,7,8,8 +After 13 days: 4,5,4,2,3,4,5,6,6,0,4,5,6,6,6,7,7,8,8 +After 14 days: 3,4,3,1,2,3,4,5,5,6,3,4,5,5,5,6,6,7,7,8 +After 15 days: 2,3,2,0,1,2,3,4,4,5,2,3,4,4,4,5,5,6,6,7 +After 16 days: 1,2,1,6,0,1,2,3,3,4,1,2,3,3,3,4,4,5,5,6,8 +After 17 days: 0,1,0,5,6,0,1,2,2,3,0,1,2,2,2,3,3,4,4,5,7,8 +After 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 diff --git a/2021/day06/input/input b/2021/day06/input/input new file mode 100644 index 0000000..1e7a8df --- /dev/null +++ b/2021/day06/input/input @@ -0,0 +1 @@ +5,1,1,4,1,1,4,1,1,1,1,1,1,1,1,1,1,1,4,2,1,1,1,3,5,1,1,1,5,4,1,1,1,2,2,1,1,1,2,1,1,1,2,5,2,1,2,2,3,1,1,1,1,1,1,1,1,5,1,1,4,1,1,1,5,4,1,1,3,3,2,1,1,1,5,1,1,4,1,1,5,1,1,5,1,2,3,1,5,1,3,2,1,3,1,1,4,1,1,1,1,2,1,2,1,1,2,1,1,1,4,4,1,5,1,1,3,5,1,1,5,1,4,1,1,1,1,1,1,1,1,1,2,2,3,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,5,1,1,1,1,4,1,1,1,1,4,1,1,1,1,3,1,2,1,2,1,3,1,3,4,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,4,1,1,2,2,1,2,4,1,1,3,1,1,1,5,1,3,1,1,1,5,5,1,1,1,1,2,3,4,1,1,1,1,1,1,1,1,1,1,1,1,5,1,4,3,1,1,1,2,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,3,3,1,2,2,1,4,1,5,1,5,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,5,1,1,1,4,3,1,1,4 diff --git a/2021/day06/src/main.rs b/2021/day06/src/main.rs new file mode 100644 index 0000000..ed253b3 --- /dev/null +++ b/2021/day06/src/main.rs @@ -0,0 +1,66 @@ +use std::collections::VecDeque; +use std::io::stdin; + +fn main() { + println!("Hello, world!"); + + let args: Vec = std::env::args().collect(); + + if args.len() > 1 && args[1] == "part1" { + part1(); + } else { + part2(); + } +} + +fn part1() { + println!("Part 1:"); + + let mut input = String::new(); + stdin().read_line(&mut input).ok(); + dbg!(&input); + + let count_fishes = count_fishes_after_days(&input, 80); + dbg!(count_fishes); +} + +fn part2() { + println!("Part 2:"); + + let mut input = String::new(); + stdin().read_line(&mut input).ok(); + dbg!(&input); + + let count_fishes = count_fishes_after_days(&input, 256); + dbg!(count_fishes); +} + +fn count_fishes_after_days(input: &str, after_days: i32) -> i64 { + let fishes: Vec = input + .split(',') + .map(|s| s.trim()) + .map(|num| num.parse().unwrap()) + .collect(); + + println!("Fishes: {:?}", fishes); + + let mut buf = VecDeque::from([0; 9]); + + for fish in fishes { + buf[fish] += 1 + } + println!("Initial state: {:?}", buf); + + for day in 1..after_days + 1 { + buf.rotate_left(1); + buf[6] += buf[8]; + let count_fishes: i64 = buf.iter().sum(); + println!( + "After {:>2} days: {:?}, {:>3} fishes", + day, buf, count_fishes + ); + } + + let count_fishes: i64 = buf.iter().sum(); + return count_fishes; +}