1
1
package com .sbaars .adventofcode .year17 .days ;
2
2
3
3
import com .sbaars .adventofcode .year17 .Day2017 ;
4
- import java .util .Arrays ;
5
- import java .util .HashSet ;
6
- import java .util .Set ;
4
+ import java .util .*;
7
5
8
6
public class Day6 extends Day2017 {
9
7
@@ -15,69 +13,66 @@ public static void main(String[] args) {
15
13
new Day6 ().printParts ();
16
14
}
17
15
16
+ private String getState (int [] banks ) {
17
+ StringBuilder sb = new StringBuilder ();
18
+ for (int bank : banks ) {
19
+ sb .append (bank ).append (',' );
20
+ }
21
+ return sb .toString ();
22
+ }
23
+
24
+ private int [] redistribute (int [] banks ) {
25
+ int [] newBanks = banks .clone ();
26
+ int maxIndex = 0 ;
27
+ int maxValue = newBanks [0 ];
28
+
29
+ // Find bank with most blocks
30
+ for (int i = 1 ; i < newBanks .length ; i ++) {
31
+ if (newBanks [i ] > maxValue ) {
32
+ maxValue = newBanks [i ];
33
+ maxIndex = i ;
34
+ }
35
+ }
36
+
37
+ // Redistribute blocks
38
+ newBanks [maxIndex ] = 0 ;
39
+ int index = (maxIndex + 1 ) % newBanks .length ;
40
+ while (maxValue > 0 ) {
41
+ newBanks [index ]++;
42
+ maxValue --;
43
+ index = (index + 1 ) % newBanks .length ;
44
+ }
45
+
46
+ return newBanks ;
47
+ }
48
+
18
49
@ Override
19
50
public Object part1 () {
20
51
int [] banks = Arrays .stream (day ().trim ().split ("\t " )).mapToInt (Integer ::parseInt ).toArray ();
21
52
Set <String > seen = new HashSet <>();
22
53
int cycles = 0 ;
23
54
24
- while (true ) {
25
- String state = Arrays .toString (banks );
26
- if (!seen .add (state )) {
27
- return cycles ;
28
- }
29
-
30
- // Find bank with most blocks
31
- int maxIndex = 0 ;
32
- for (int i = 1 ; i < banks .length ; i ++) {
33
- if (banks [i ] > banks [maxIndex ]) {
34
- maxIndex = i ;
35
- }
36
- }
37
-
38
- // Redistribute blocks
39
- int blocks = banks [maxIndex ];
40
- banks [maxIndex ] = 0 ;
41
- for (int i = 0 ; i < blocks ; i ++) {
42
- banks [(maxIndex + 1 + i ) % banks .length ]++;
43
- }
44
-
55
+ while (seen .add (getState (banks ))) {
56
+ banks = redistribute (banks );
45
57
cycles ++;
46
58
}
59
+
60
+ return cycles ;
47
61
}
48
62
49
63
@ Override
50
64
public Object part2 () {
51
65
int [] banks = Arrays .stream (day ().trim ().split ("\t " )).mapToInt (Integer ::parseInt ).toArray ();
52
- Set <String > seen = new HashSet <>();
66
+ Map <String , Integer > seen = new HashMap <>();
53
67
int cycles = 0 ;
54
- String firstRepeat = null ;
55
68
56
69
while (true ) {
57
- String state = Arrays .toString (banks );
58
- if (state .equals (firstRepeat )) {
59
- return cycles ;
60
- }
61
- if (!seen .add (state )) {
62
- firstRepeat = state ;
63
- cycles = 0 ;
64
- }
65
-
66
- // Find bank with most blocks
67
- int maxIndex = 0 ;
68
- for (int i = 1 ; i < banks .length ; i ++) {
69
- if (banks [i ] > banks [maxIndex ]) {
70
- maxIndex = i ;
71
- }
72
- }
73
-
74
- // Redistribute blocks
75
- int blocks = banks [maxIndex ];
76
- banks [maxIndex ] = 0 ;
77
- for (int i = 0 ; i < blocks ; i ++) {
78
- banks [(maxIndex + 1 + i ) % banks .length ]++;
70
+ String state = getState (banks );
71
+ if (seen .containsKey (state )) {
72
+ return cycles - seen .get (state );
79
73
}
80
-
74
+ seen .put (state , cycles );
75
+ banks = redistribute (banks );
81
76
cycles ++;
82
77
}
83
78
}
0 commit comments