Skip to content

Instantly share code, notes, and snippets.

@roccodev
Last active December 6, 2017 13:00
Show Gist options
  • Select an option

  • Save roccodev/4487e467f7860156e1de29a1e1c07109 to your computer and use it in GitHub Desktop.

Select an option

Save roccodev/4487e467f7860156e1de29a1e1c07109 to your computer and use it in GitHub Desktop.
AdventOfCode2017/Day6
public static void main(String[] args) throws IOException {
List<Integer> nums = Arrays.stream(Files.readAllLines(Paths.get(new File("res/day6").getPath())).get(0)
.split("\\s+"))
.mapToInt(Integer::parseInt)
.boxed()
.collect(Collectors.toList());
Map<List<Integer>, Integer> seen = new HashMap<>();
seen.put(nums, 0);
int steps = 0;
while (true) {
int index = firstMax(nums);
int redistribute = nums.get(index);
nums.set(index++, 0);
while (redistribute-- > 0) {
index %= nums.size();
nums.set(index, nums.get(index++) + 1);
}
steps++;
if (seen.containsKey(nums)) {
break;
}
seen.put(nums, steps);
}
}
private static int firstMax(List<Integer> a) {
int max = Collections.max(a);
for (int i = 0; i < a.size(); i++) {
if (a.get(i).equals(max)) return i;
}
return 0;
}
public static void main(String[] args) throws IOException {
List<Integer> nums = Arrays.stream(Files.readAllLines(Paths.get(new File("res/day6").getPath())).get(0)
.split("\\s+"))
.mapToInt(Integer::parseInt)
.boxed()
.collect(Collectors.toList());
Map<List<Integer>, Integer> seen = new HashMap<>();
seen.put(nums, 0);
int steps = 0;
while (true) {
int index = firstMax(nums);
int redistribute = nums.get(index);
nums.set(index++, 0);
while (redistribute-- > 0) {
index %= nums.size();
nums.set(index, nums.get(index++) + 1);
}
steps++;
if (seen.containsKey(nums)) {
System.out.println("Steps: " + (steps - seen.get(nums)));
break;
}
seen.put(nums, steps);
}
}
private static int firstMax(List<Integer> a) {
int max = Collections.max(a);
for (int i = 0; i < a.size(); i++) {
if (a.get(i).equals(max)) return i;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment