Day 6
This commit is contained in:
parent
b26a3830d2
commit
4b4d4bdadd
3 changed files with 40 additions and 0 deletions
38
src/bin/06.rs
Normal file
38
src/bin/06.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
type School = VecDeque<u64>;
|
||||
|
||||
fn main() {
|
||||
let mut fish = input();
|
||||
for _ in 0..80 {
|
||||
step(&mut fish);
|
||||
}
|
||||
println!("First solution: {}", sum(&fish));
|
||||
|
||||
let mut fish = input();
|
||||
for _ in 0..256 {
|
||||
step(&mut fish);
|
||||
}
|
||||
println!("Second solution: {}", sum(&fish));
|
||||
}
|
||||
|
||||
fn step(fish: &mut School) {
|
||||
let front = fish.pop_front().unwrap();
|
||||
fish.push_back(front);
|
||||
fish[6] += front;
|
||||
}
|
||||
|
||||
fn sum(fish: &School) -> u64 {
|
||||
fish.iter().sum()
|
||||
}
|
||||
|
||||
fn input() -> School {
|
||||
let init: School = vec![0; 9].into();
|
||||
include_str!("../../input/06.txt")
|
||||
.split(',')
|
||||
.map(|n| n.parse().unwrap())
|
||||
.fold(init, |mut acc, n| {
|
||||
acc[n] += 1;
|
||||
acc
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue