File tree 1 file changed +8
-15
lines changed
1 file changed +8
-15
lines changed Original file line number Diff line number Diff line change 1
- use std :: collections :: HashSet ;
1
+ const BANK_QTY : usize = 16 ;
2
2
3
3
fn main ( ) {
4
4
let input = "5 1 10 0 1 7 13 14 3 12 8 10 7 12 0 6" ;
5
- const BANK_QTY : usize = 16 ;
6
5
7
6
// setup memory banks
8
7
let mut banks: [ u32 ; BANK_QTY ] = [ 0 ; BANK_QTY ] ;
@@ -12,22 +11,16 @@ fn main() {
12
11
banks[ index] = element. parse :: < u32 > ( ) . unwrap ( ) ;
13
12
}
14
13
15
- let index = get_max_index ( & banks) ;
16
-
17
- let mut set = HashSet :: new ( ) ;
18
- let mut iteration = 0 ;
19
-
14
+ let mut history: Vec < _ > = Vec :: new ( ) ;
20
15
21
16
let result = loop {
22
- // println!("{:?}", (banks));
23
-
24
- // add array to hashset and check lengths
25
- set. insert ( banks. clone ( ) ) ;
26
- iteration += 1 ;
27
-
28
- if iteration != set. len ( ) {
29
- break set. len ( ) ;
17
+ // check if prev exists
18
+ if history. iter ( ) . any ( |& d| d == banks) {
19
+ let pos = history. iter ( ) . position ( |& p| p == banks) . unwrap ( ) ;
20
+ break ( pos, banks, history. iter ( ) . count ( ) - pos) ;
30
21
}
22
+ // count it
23
+ history. push ( banks. clone ( ) ) ;
31
24
32
25
// iterate
33
26
let index = get_max_index ( & banks) ;
You can’t perform that action at this time.
0 commit comments