-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |