Last active
December 13, 2017 08:43
-
-
Save snarkbait/e6812b39ce04af2c18db9dfd95e1ea10 to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Day 13
This file contains 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
package Advent2017; | |
import util.FileIO; | |
import util.Timer; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
public class Day13 { | |
public static void main(String[] args) { | |
Map<Integer, Integer> map; | |
List<int[]> input = FileIO.getFileLinesSplitAsInt("advent2017_day13.txt", ": "); | |
Timer.startTimer(); | |
map = input.stream() | |
.collect(Collectors.toMap(x -> x[0], x -> x[1])); | |
int severity = map.entrySet().stream() | |
.filter(x -> x.getKey() % ((x.getValue() - 1) * 2) == 0) | |
.mapToInt(x -> x.getKey() * x.getValue()) | |
.sum(); | |
System.out.println("Part 1:" + severity); | |
int delay = 2; | |
while (true) { | |
boolean caught = false; | |
for (Map.Entry<Integer, Integer> layer : map.entrySet()) { | |
if ((layer.getKey() + delay) % ((layer.getValue() - 1) * 2) == 0) { | |
caught = true; | |
break; | |
} | |
} | |
if (!caught) break; | |
delay+= 2; | |
} | |
System.out.println("Part 2: " + delay); | |
System.out.println(Timer.endTimer()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment