diff --git a/2021/day6/Cargo.lock b/2021/day6/Cargo.lock new file mode 100644 index 0000000..a16c0bf --- /dev/null +++ b/2021/day6/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day6" +version = "0.1.0" diff --git a/2021/day6/Cargo.toml b/2021/day6/Cargo.toml new file mode 100644 index 0000000..89d04ae --- /dev/null +++ b/2021/day6/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day6" +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/day6/input.txt b/2021/day6/input.txt new file mode 100644 index 0000000..90498da --- /dev/null +++ b/2021/day6/input.txt @@ -0,0 +1 @@ +1,2,5,1,1,4,1,5,5,5,3,4,1,2,2,5,3,5,1,3,4,1,5,2,5,1,4,1,2,2,1,5,1,1,1,2,4,3,4,2,2,4,5,4,1,2,3,5,3,4,1,1,2,2,1,3,3,2,3,2,1,2,2,3,1,1,2,5,1,2,1,1,3,1,1,5,5,4,1,1,5,1,4,3,5,1,3,3,1,1,5,2,1,2,4,4,5,5,4,4,5,4,3,5,5,1,3,5,2,4,1,1,2,2,2,4,1,2,1,5,1,3,1,1,1,2,1,2,2,1,3,3,5,3,4,2,1,5,2,1,4,1,1,5,1,1,5,4,4,1,4,2,3,5,2,5,5,2,2,4,4,1,1,1,4,4,1,3,5,4,2,5,5,4,4,2,2,3,2,1,3,4,1,5,1,4,5,2,4,5,1,3,4,1,4,3,3,1,1,3,2,1,5,5,3,1,1,2,4,5,3,1,1,1,2,5,2,4,5,1,3,2,4,5,5,1,2,3,4,4,1,4,1,1,3,3,5,1,2,5,1,2,5,4,1,1,3,2,1,1,1,3,5,1,3,2,4,3,5,4,1,1,5,3,4,2,3,1,1,4,2,1,2,2,1,1,4,3,1,1,3,5,2,1,3,2,1,1,1,2,1,1,5,1,1,2,5,1,1,4 diff --git a/2021/day6/src/fish.rs b/2021/day6/src/fish.rs new file mode 100644 index 0000000..3ec208c --- /dev/null +++ b/2021/day6/src/fish.rs @@ -0,0 +1,48 @@ +const DAYS_TO_CREATE_FISH: u32 = 6; +const NEW_FISH_EXTRA_DAYS: u32 = 2; + +pub fn simulate_spawn(input: &str, days_to_simulate: usize) -> usize { + let mut fish_school = input + .replace(" ", "") + .split('\n') + .filter(|line| *line != "") + .collect::>() + .first() + .unwrap() + .split(',') + .map(|s| s.parse().unwrap()) + .collect::>(); + + (0..days_to_simulate).for_each(|_| { + let mut spawned_fish: Vec = vec![]; + fish_school.iter_mut().for_each(|fish| { + if *fish == 0 { + *fish = DAYS_TO_CREATE_FISH; + spawned_fish.push(DAYS_TO_CREATE_FISH + NEW_FISH_EXTRA_DAYS); + } else { + *fish -= 1; + } + }); + + fish_school.append(&mut spawned_fish); + }); + + fish_school.len() +} + +#[cfg(test)] +mod tests { + + use crate::fish::*; + + #[test] + fn test_input_example() { + let example = "3,4,3,1,2"; + + let actual_num_fish_after_18_days = simulate_spawn(example, 18); + assert_eq!(actual_num_fish_after_18_days, 26); + + let actual_num_fish_after_80_days = simulate_spawn(example, 80); + assert_eq!(actual_num_fish_after_80_days, 5934); + } +} diff --git a/2021/day6/src/main.rs b/2021/day6/src/main.rs new file mode 100644 index 0000000..2402fa6 --- /dev/null +++ b/2021/day6/src/main.rs @@ -0,0 +1,10 @@ +use std::fs; + +pub mod fish; + +fn main() { + let input = fs::read_to_string("input.txt").expect("Something went wrong reading the file"); + + let num_fish = fish::simulate_spawn(&input[..], 80); + println!("Num fish after 80 days = {}", num_fish); +}