2021-12-06 11:01:23 +01:00
#![ feature(test) ]
extern crate test ;
use aoc2021 ::common ::* ;
2021-12-06 14:09:46 +01:00
use std ::iter ;
2021-12-06 11:01:23 +01:00
const DAY : usize = 6 ;
2021-12-06 12:26:12 +01:00
const FERTILITY_CYCLE : usize = 7 ;
2021-12-06 14:09:46 +01:00
const INITIAL_DELAY : usize = FERTILITY_CYCLE + 2 ;
2021-12-06 11:01:23 +01:00
type Parsed = Vec < usize > ;
fn parse_input ( raw : & str ) -> Parsed {
parse_nums_comma ( raw )
}
2021-12-06 12:26:12 +01:00
fn simulate < const LIMIT : usize > ( parsed : & Parsed ) -> usize {
2021-12-06 14:09:46 +01:00
let next_fertile_day = | day : & usize | Some ( day + FERTILITY_CYCLE ) . filter ( | n | n < & LIMIT ) ;
2021-12-06 12:26:12 +01:00
let mut fish_from_day = [ 1 ; LIMIT ] ;
2021-12-06 14:09:46 +01:00
for i in ( 0 .. LIMIT - INITIAL_DELAY ) . rev ( ) {
fish_from_day [ i ] + = iter ::successors ( Some ( i + INITIAL_DELAY ) , next_fertile_day ) . map ( | n | fish_from_day [ n ] ) . sum ::< usize > ( )
2021-12-06 12:26:12 +01:00
}
2021-12-06 14:09:46 +01:00
let adult_from_day : Vec < usize > =
( 0 .. FERTILITY_CYCLE ) . map ( | i | iter ::successors ( Some ( i ) , next_fertile_day ) . map ( | i | fish_from_day [ i ] ) . sum ( ) ) . collect ( ) ;
2021-12-06 13:05:57 +01:00
parsed . iter ( ) . map ( | & i | 1 + adult_from_day [ i ] ) . sum ( )
2021-12-06 11:01:23 +01:00
}
fn main ( ) {
let input = parse_input ( & read_file ( DAY ) ) ;
2021-12-06 12:26:12 +01:00
println! ( " Part 1: {} " , simulate ::< 80 > ( & input ) ) ;
println! ( " Part 2: {} " , simulate ::< 256 > ( & input ) ) ;
2021-12-06 11:01:23 +01:00
}
#[ cfg(test) ]
mod tests {
use super ::* ;
use aoc2021 ::* ;
const TEST_INPUT : & str = " 3,4,3,1,2 " ;
2021-12-06 12:26:12 +01:00
test! ( simulate < 80 > ( ) = = 5934 ) ;
test! ( simulate < 256 > ( ) = = 26984457539 ) ;
bench! ( simulate < 80 > ( ) = = 365862 ) ;
bench! ( simulate < 256 > ( ) = = 1653250886439 ) ;
bench_input! ( Vec ::len = > 300 ) ;
2021-12-06 11:01:23 +01:00
}