2021-12-21 09:38:17 +01:00
#![ feature(test) ]
extern crate test ;
2021-12-21 15:45:38 +01:00
type Parsed = ( u16 , u16 ) ;
const INPUT : Parsed = ( 7 , 3 ) ;
fn part1 ( ( p1 , p2 ) : Parsed ) -> usize {
( 1 ..= 100 )
. cycle ( )
2021-12-21 16:02:44 +01:00
. skip ( 1 )
. step_by ( 3 )
2021-12-21 15:45:38 +01:00
. enumerate ( )
2021-12-21 16:02:44 +01:00
. scan ( [ ( p1 , 0 ) , ( p2 , 0 ) ] , | mut scores , ( round , die ) | {
let mut points = scores [ round & 1 ] . 0 + die * 3 ;
// can’t use mod 10 because we actually want to keep the 10
while points > 10 {
points - = 10 ;
2021-12-21 15:45:38 +01:00
}
scores [ round & 1 ] . 0 = points ;
scores [ round & 1 ] . 1 + = points ;
2021-12-21 16:02:44 +01:00
Some ( ( round + 1 , * scores ) )
2021-12-21 15:45:38 +01:00
} )
2021-12-21 16:02:44 +01:00
. find_map ( | ( r , [ ( _ , s1 ) , ( _ , s2 ) ] ) | ( s1 > = 1000 | | s2 > = 1000 ) . then ( | | r * 3 * ( s1 . min ( s2 ) as usize ) ) )
2021-12-21 15:45:38 +01:00
. unwrap ( )
2021-12-21 09:38:17 +01:00
}
2021-12-21 15:45:38 +01:00
fn part2 ( ( p1 , p2 ) : Parsed ) -> usize {
2021-12-21 09:38:17 +01:00
unimplemented! ( )
}
fn main ( ) {
2021-12-21 15:45:38 +01:00
println! ( " Part 1: {} " , part1 ( INPUT ) ) ;
println! ( " Part 2: {} " , part2 ( INPUT ) ) ;
2021-12-21 09:38:17 +01:00
}
#[ cfg(test) ]
mod tests {
use super ::* ;
use aoc2021 ::* ;
2021-12-21 15:45:38 +01:00
#[ test ]
fn part1_test ( ) {
assert_eq! ( part1 ( ( 4 , 8 ) ) , 739785 ) ;
}
2021-12-21 16:02:44 +01:00
#[ bench ]
fn part1_bench ( b : & mut test ::Bencher ) {
b . iter ( | | assert_eq! ( part1 ( test ::black_box ( INPUT ) ) , 551901 ) )
}
2021-12-21 09:38:17 +01:00
}