Skip to content

Commit

Permalink
Add simulate_fish
Browse files Browse the repository at this point in the history
  • Loading branch information
tjheslin1 committed Dec 19, 2021
1 parent 08cede1 commit 15787ea
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 2021/day6/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 2021/day6/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
1 change: 1 addition & 0 deletions 2021/day6/input.txt
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions 2021/day6/src/fish.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<&str>>()
.first()
.unwrap()
.split(',')
.map(|s| s.parse().unwrap())
.collect::<Vec<u32>>();

(0..days_to_simulate).for_each(|_| {
let mut spawned_fish: Vec<u32> = 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);
}
}
10 changes: 10 additions & 0 deletions 2021/day6/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 15787ea

Please sign in to comment.