1
-
2
1
use crate :: util;
3
2
4
3
pub fn run ( ) {
5
4
let raw_input = util:: read_input ( "inputs/day6.txt" ) . unwrap ( ) ;
6
5
let starting_statte = process ( & raw_input) ;
7
6
println ! ( "Part 1: {:?}" , part_1( starting_statte) ) ;
8
7
println ! ( "Part 2: {:?}" , part_2( starting_statte) ) ;
9
-
10
8
}
11
9
12
10
fn process ( input : & str ) -> [ u64 ; 9 ] {
13
- let mut tracker: [ u64 ; 9 ] = [ 0 ; 9 ] ;
14
- input. trim ( ) . split ( ',' )
11
+ let mut tracker: [ u64 ; 9 ] = [ 0 ; 9 ] ;
12
+ input
13
+ . trim ( )
14
+ . split ( ',' )
15
15
. map ( |day_str| day_str. parse :: < u64 > ( ) . unwrap ( ) )
16
16
. for_each ( |day| tracker[ day as usize ] += 1 ) ;
17
17
tracker
@@ -26,19 +26,18 @@ fn part_2(state: [u64; 9]) -> usize {
26
26
}
27
27
28
28
fn process_generations ( state : [ u64 ; 9 ] , num_generations : usize ) -> usize {
29
- let final_state: [ u64 ; 9 ] = ( 0 ..num_generations)
30
- . fold ( state, |mut curr_state, _| {
31
- let mut temp_state: [ u64 ; 9 ] = [ 0 ; 9 ] ;
32
- temp_state. copy_from_slice ( & curr_state) ;
29
+ let final_state: [ u64 ; 9 ] = ( 0 ..num_generations) . fold ( state, |mut curr_state, _| {
30
+ let mut temp_state: [ u64 ; 9 ] = [ 0 ; 9 ] ;
31
+ temp_state. copy_from_slice ( & curr_state) ;
33
32
34
- for bucket in 1 ..9 {
35
- curr_state[ bucket- 1 ] = temp_state[ bucket] ;
36
- }
33
+ for bucket in 1 ..9 {
34
+ curr_state[ bucket - 1 ] = temp_state[ bucket] ;
35
+ }
37
36
38
- curr_state[ 6 ] += temp_state[ 0 ] ;
39
- curr_state[ 8 ] = temp_state[ 0 ] ;
40
- curr_state
41
- } ) ;
37
+ curr_state[ 6 ] += temp_state[ 0 ] ;
38
+ curr_state[ 8 ] = temp_state[ 0 ] ;
39
+ curr_state
40
+ } ) ;
42
41
final_state. iter ( ) . map ( |count| * count as usize ) . sum ( )
43
42
}
44
43
0 commit comments