1
1
use crate :: solutions:: Solution ;
2
+ use itertools:: Itertools ;
2
3
3
4
pub struct Day22 ;
4
5
@@ -16,15 +17,26 @@ impl Solution for Day22 {
16
17
. to_string ( )
17
18
}
18
19
19
- fn part_two ( & self , _input : & str ) -> String {
20
+ fn part_two ( & self , input : & str ) -> String {
21
+ let _diffs: Vec < Vec < i8 > > = input
22
+ . lines ( )
23
+ . map ( |line| {
24
+ let initial: usize = line. parse ( ) . unwrap ( ) ;
25
+ let secrets = self . next_secrets ( initial, 2000 ) ;
26
+ let prices = self . prices ( secrets) ;
27
+
28
+ self . diffs ( prices)
29
+ } )
30
+ . collect ( ) ;
31
+
20
32
String :: from ( "0" )
21
33
}
22
34
}
23
35
24
36
impl Day22 {
25
37
fn next_secrets ( & self , initial : usize , number_of_secrets : usize ) -> Vec < usize > {
26
38
let mut secret = initial;
27
- let mut next_secrets = Vec :: new ( ) ;
39
+ let mut next_secrets = vec ! [ initial ] ;
28
40
29
41
for _ in 0 ..number_of_secrets {
30
42
secret = self . mix_and_prune ( secret, |s| s * 64 ) ;
@@ -40,38 +52,68 @@ impl Day22 {
40
52
fn mix_and_prune ( & self , current : usize , calculations : fn ( usize ) -> usize ) -> usize {
41
53
( current ^ calculations ( current) ) % 16777216
42
54
}
55
+
56
+ fn prices ( & self , secrets : Vec < usize > ) -> Vec < i8 > {
57
+ secrets. iter ( ) . map ( |secret| ( secret % 10 ) as i8 ) . collect ( )
58
+ }
59
+
60
+ fn diffs ( & self , secrets : Vec < i8 > ) -> Vec < i8 > {
61
+ secrets. iter ( ) . tuple_windows ( ) . map ( |( a, b) | b - a) . collect ( )
62
+ }
43
63
}
44
64
45
65
#[ cfg( test) ]
46
66
mod tests {
47
67
use crate :: solutions:: year2024:: day22:: Day22 ;
48
68
use crate :: solutions:: Solution ;
49
69
50
- const EXAMPLE : & str = r#"1
70
+ const PART_ONE_EXAMPLE : & str = r#"1
51
71
10
52
72
100
53
73
2024"# ;
54
74
55
75
#[ test]
56
76
fn part_one_example ( ) {
57
- assert_eq ! ( "37327623" , Day22 . part_one( EXAMPLE ) ) ;
77
+ assert_eq ! ( "37327623" , Day22 . part_one( PART_ONE_EXAMPLE ) ) ;
78
+ }
79
+
80
+ const PART_TWO_EXAMPLE : & str = r#"1
81
+ 2
82
+ 3
83
+ 2024"# ;
84
+
85
+ #[ test]
86
+ #[ ignore]
87
+ fn part_two_example ( ) {
88
+ assert_eq ! ( "23" , Day22 . part_two( PART_TWO_EXAMPLE ) ) ;
58
89
}
59
90
60
91
#[ test]
61
92
fn next_secrets ( ) {
62
- let tmp = Day22 . next_secrets ( 123 , 10 ) ;
63
- let mut result = tmp. iter ( ) ;
64
-
65
- assert_eq ! ( Some ( & 15887950 ) , result. next( ) ) ;
66
- assert_eq ! ( Some ( & 16495136 ) , result. next( ) ) ;
67
- assert_eq ! ( Some ( & 527345 ) , result. next( ) ) ;
68
- assert_eq ! ( Some ( & 704524 ) , result. next( ) ) ;
69
- assert_eq ! ( Some ( & 1553684 ) , result. next( ) ) ;
70
- assert_eq ! ( Some ( & 12683156 ) , result. next( ) ) ;
71
- assert_eq ! ( Some ( & 11100544 ) , result. next( ) ) ;
72
- assert_eq ! ( Some ( & 12249484 ) , result. next( ) ) ;
73
- assert_eq ! ( Some ( & 7753432 ) , result. next( ) ) ;
74
- assert_eq ! ( Some ( & 5908254 ) , result. next( ) ) ;
75
- assert_eq ! ( None , result. next( ) ) ;
93
+ let secrets = Day22 . next_secrets ( 123 , 10 ) ;
94
+ let mut iter = secrets. iter ( ) ;
95
+
96
+ assert_eq ! ( Some ( & 123 ) , iter. next( ) ) ;
97
+ assert_eq ! ( Some ( & 15887950 ) , iter. next( ) ) ;
98
+ assert_eq ! ( Some ( & 16495136 ) , iter. next( ) ) ;
99
+ assert_eq ! ( Some ( & 527345 ) , iter. next( ) ) ;
100
+ assert_eq ! ( Some ( & 704524 ) , iter. next( ) ) ;
101
+ assert_eq ! ( Some ( & 1553684 ) , iter. next( ) ) ;
102
+ assert_eq ! ( Some ( & 12683156 ) , iter. next( ) ) ;
103
+ assert_eq ! ( Some ( & 11100544 ) , iter. next( ) ) ;
104
+ assert_eq ! ( Some ( & 12249484 ) , iter. next( ) ) ;
105
+ assert_eq ! ( Some ( & 7753432 ) , iter. next( ) ) ;
106
+ assert_eq ! ( Some ( & 5908254 ) , iter. next( ) ) ;
107
+ assert_eq ! ( None , iter. next( ) ) ;
108
+ }
109
+
110
+ #[ test]
111
+ fn prices_and_diffs ( ) {
112
+ let secrets = Day22 . next_secrets ( 123 , 9 ) ;
113
+ let prices = Day22 . prices ( secrets) ;
114
+ assert_eq ! ( vec![ 3 , 0 , 6 , 5 , 4 , 4 , 6 , 4 , 4 , 2 ] , prices) ;
115
+
116
+ let diffs = Day22 . diffs ( prices) ;
117
+ assert_eq ! ( vec![ -3 , 6 , -1 , -1 , 0 , 2 , -2 , 0 , -2 ] , diffs) ;
76
118
}
77
119
}
0 commit comments