Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Last active December 6, 2017 07:37
Show Gist options
  • Save snarkbait/7f55a367f6ef3c247b6b570e081827f1 to your computer and use it in GitHub Desktop.
Save snarkbait/7f55a367f6ef3c247b6b570e081827f1 to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Day 6
package Advent2017;
import util.FileIO;
import java.util.*;
import java.util.stream.Collectors;
public class Day6 {
private static int firstMax(List<Integer> a) {
int max = Collections.max(a);
return a.indexOf(max);
}
public static void main(String[] args) {
List<Integer> nums = Arrays.stream(FileIO.getFileAsString("advent2017_day6.txt")
.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("Part 2: " + (steps - seen.get(nums)));
break;
}
seen.put(nums, steps);
}
System.out.println("Part 1: " + steps);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment