mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement day 6 2021
This commit is contained in:
1
2021/inputs/06.txt
Normal file
1
2021/inputs/06.txt
Normal file
@@ -0,0 +1 @@
|
||||
1,4,2,4,5,3,5,2,2,5,2,1,2,4,5,2,3,5,4,3,3,1,2,3,2,1,4,4,2,1,1,4,1,4,4,4,1,4,2,4,3,3,3,3,1,1,5,4,2,5,2,4,2,2,3,1,2,5,2,4,1,5,3,5,1,4,5,3,1,4,5,2,4,5,3,1,2,5,1,2,2,1,5,5,1,1,1,4,2,5,4,3,3,1,3,4,1,1,2,2,2,5,4,4,3,2,1,1,1,1,2,5,1,3,2,1,4,4,2,1,4,5,2,5,5,3,3,1,3,2,2,3,4,1,3,1,5,4,2,5,2,4,1,5,1,4,5,1,2,4,4,1,4,1,4,4,2,2,5,4,1,3,1,3,3,1,5,1,5,5,5,1,3,1,2,1,4,5,4,4,1,3,3,1,4,1,2,1,3,2,1,5,5,3,3,1,3,5,1,5,3,5,3,1,1,1,1,4,4,3,5,5,1,1,2,2,5,5,3,2,5,2,3,4,4,1,1,2,2,4,3,5,5,1,1,5,4,3,1,3,1,2,4,4,4,4,1,4,3,4,1,3,5,5,5,1,3,5,4,3,1,3,5,4,4,3,4,2,1,1,3,1,1,2,4,1,4,1,1,1,5,5,1,3,4,1,1,5,4,4,2,2,1,3,4,4,2,2,2,3
|
||||
@@ -1,9 +1,63 @@
|
||||
use std::io::Read;
|
||||
|
||||
pub fn part1(_input: &mut dyn Read) -> String {
|
||||
todo!()
|
||||
fn fish_growth(fish: &[u8], days: usize) -> usize {
|
||||
let mut fish_per_day = [0usize; 9];
|
||||
|
||||
for &life in fish {
|
||||
fish_per_day[life as usize] += 1;
|
||||
}
|
||||
|
||||
for day in 0..days {
|
||||
let index = day % fish_per_day.len();
|
||||
let offspring_today = fish_per_day[index];
|
||||
|
||||
// The parents can be parents in 6 days
|
||||
fish_per_day[(index + 7) % fish_per_day.len()] += offspring_today;
|
||||
// The offspring from today will be ready the next time they come around
|
||||
}
|
||||
|
||||
fish_per_day.into_iter().sum()
|
||||
}
|
||||
|
||||
pub fn part2(_input: &mut dyn Read) -> String {
|
||||
todo!()
|
||||
fn part_common(input: &mut dyn Read, days: usize) -> String {
|
||||
let mut buffer = String::new();
|
||||
input.read_to_string(&mut buffer).unwrap();
|
||||
|
||||
let fish: Vec<u8> = buffer
|
||||
.trim_end()
|
||||
.split(',')
|
||||
.map(|s| s.parse().unwrap())
|
||||
.collect();
|
||||
|
||||
fish_growth(&fish, days).to_string()
|
||||
}
|
||||
|
||||
pub fn part1(input: &mut dyn Read) -> String {
|
||||
part_common(input, 80)
|
||||
}
|
||||
|
||||
pub fn part2(input: &mut dyn Read) -> String {
|
||||
part_common(input, 256)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const SAMPLE: [u8; 5] = [3, 4, 3, 1, 2];
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
assert_eq!(fish_growth(&SAMPLE, 1), 5);
|
||||
assert_eq!(fish_growth(&SAMPLE, 2), 6);
|
||||
assert_eq!(fish_growth(&SAMPLE, 3), 7);
|
||||
assert_eq!(fish_growth(&SAMPLE, 4), 9);
|
||||
assert_eq!(fish_growth(&SAMPLE, 18), 26);
|
||||
assert_eq!(fish_growth(&SAMPLE, 80), 5934);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_part2() {
|
||||
assert_eq!(fish_growth(&SAMPLE, 256), 26984457539);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user