Last active
December 6, 2017 13:00
-
-
Save roccodev/4487e467f7860156e1de29a1e1c07109 to your computer and use it in GitHub Desktop.
AdventOfCode2017/Day6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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