Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d21d9b5

Browse files
Fixed 2017 Day 6 implementation - improved efficiency and fixed cycle detection
1 parent 931dda5 commit d21d9b5

File tree

1 file changed

+44
-49
lines changed
  • src/main/java/com/sbaars/adventofcode/year17/days

1 file changed

+44
-49
lines changed

‎src/main/java/com/sbaars/adventofcode/year17/days/Day6.java‎

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.sbaars.adventofcode.year17.days;
22

33
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.*;
75

86
public class Day6 extends Day2017 {
97

@@ -15,69 +13,66 @@ public static void main(String[] args) {
1513
new Day6().printParts();
1614
}
1715

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+
1849
@Override
1950
public Object part1() {
2051
int[] banks = Arrays.stream(day().trim().split("\t")).mapToInt(Integer::parseInt).toArray();
2152
Set<String> seen = new HashSet<>();
2253
int cycles = 0;
2354

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);
4557
cycles++;
4658
}
59+
60+
return cycles;
4761
}
4862

4963
@Override
5064
public Object part2() {
5165
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<>();
5367
int cycles = 0;
54-
String firstRepeat = null;
5568

5669
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);
7973
}
80-
74+
seen.put(state, cycles);
75+
banks = redistribute(banks);
8176
cycles++;
8277
}
8378
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /