This commit is contained in:
Zoé Cassiopée Gauthier 2021-12-06 11:51:29 -05:00
parent 0728025d0b
commit 9aa5eb3727
3 changed files with 60 additions and 0 deletions

View File

@ -22,3 +22,7 @@ path = "src/day4.rs"
[[bin]]
name = "day5"
path = "src/day5.rs"
[[bin]]
name = "day6"
path = "src/day6.rs"

1
inputs/day6.txt Normal file
View File

@ -0,0 +1 @@
5,1,5,3,2,2,3,1,1,4,2,4,1,2,1,4,1,1,5,3,5,1,5,3,1,2,4,4,1,1,3,1,1,3,1,1,5,1,5,4,5,4,5,1,3,2,4,3,5,3,5,4,3,1,4,3,1,1,1,4,5,1,1,1,2,1,2,1,1,4,1,4,1,1,3,3,2,2,4,2,1,1,5,3,1,3,1,1,4,3,3,3,1,5,2,3,1,3,1,5,2,2,1,2,1,1,1,3,4,1,1,1,5,4,1,1,1,4,4,2,1,5,4,3,1,2,5,1,1,1,1,2,1,5,5,1,1,1,1,3,1,4,1,3,1,5,1,1,1,5,5,1,4,5,4,5,4,3,3,1,3,1,1,5,5,5,5,1,2,5,4,1,1,1,2,2,1,3,1,1,2,4,2,2,2,1,1,2,2,1,5,2,1,1,2,1,3,1,3,2,2,4,3,1,2,4,5,2,1,4,5,4,2,1,1,1,5,4,1,1,4,1,4,3,1,2,5,2,4,1,1,5,1,5,4,1,1,4,1,1,5,5,1,5,4,2,5,2,5,4,1,1,4,1,2,4,1,2,2,2,1,1,1,5,5,1,2,5,1,3,4,1,1,1,1,5,3,4,1,1,2,1,1,3,5,5,2,3,5,1,1,1,5,4,3,4,2,2,1,3

55
src/day6.rs Normal file
View File

@ -0,0 +1,55 @@
fn main() {
const INPUT: &'static str = include_str!("../inputs/day6.txt");
let fishes: Vec<usize> = INPUT
.strip_suffix('\n')
.unwrap()
.split(',')
.map(|timer| timer.parse::<usize>().unwrap())
.collect();
println!("solution {}", solve(fishes.clone(), 80));
println!("solution {}", solve(fishes.clone(), 256));
}
const BIRTH_RATE: usize = 7;
const MATURITY: usize = 9;
fn solve(fishes: Vec<usize>, days: usize) -> u64 {
let mut timers = [0u64; BIRTH_RATE];
let mut new_timers = [0u64; MATURITY];
for fish in fishes {
timers[fish] += 1;
}
for day in 0..days {
let spawns = timers[day % BIRTH_RATE];
let adults = new_timers[day % MATURITY];
new_timers[day % MATURITY] += spawns;
timers[day % BIRTH_RATE] += adults;
}
timers.iter().sum::<u64>() + new_timers.iter().sum::<u64>()
}
#[test]
fn test_initial_state() {
let fishes = vec![3, 4, 3, 1, 2];
assert_eq!(solve(fishes, 0), 5);
}
#[test]
fn test_18days() {
let fishes = vec![3, 4, 3, 1, 2];
assert_eq!(solve(fishes, 18), 26);
}
#[test]
fn test_80days() {
let fishes = vec![3, 4, 3, 1, 2];
assert_eq!(solve(fishes, 80), 5934);
}
#[test]
fn test_256days() {
let fishes = vec![3, 4, 3, 1, 2];
assert_eq!(solve(fishes, 256), 26984457539);
}