Skip to content
Snippets Groups Projects
Commit 751e4d08 authored by LUDMANN Pierre's avatar LUDMANN Pierre
Browse files

[day8] there is a lie but i have been spoiled

parent c77e4d58
Branches day8-ludmann1
No related tags found
No related merge requests found
package CieDuVaseDesNoces;
import org.apache.commons.math3.util.ArithmeticUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
......@@ -8,7 +10,6 @@ import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static java.lang.Thread.currentThread;
......@@ -26,8 +27,9 @@ class Day8 extends Day {
reader.lines().forEach(line -> {
Matcher nodeMatcher = nodePattern.matcher(line);
if (!nodeMatcher.matches()) return;
network.put(nodeMatcher.group("from"), new ElementMap(
nodeMatcher.group("left"), nodeMatcher.group("right")));
network.put(nodeMatcher.group("from"),
new ElementMap(nodeMatcher.group("left"),
nodeMatcher.group("right")));
});
} catch (IOException e) {
throw new RuntimeException(e);
......@@ -35,21 +37,32 @@ class Day8 extends Day {
}
String part1() {
return part1("AAA", "ZZZ");
return part1("AAA", "Z$");
}
String part1(String startLabel, String targetLabel) {
String part1(String startLabel, String targetRegex) {
String label = startLabel;
Pattern targetPattern = Pattern.compile(targetRegex);
int step = 0;
int length = instructions.length;
while (!label.equals(targetLabel)) {
label = network.get(label).get(instructions[step++ % length]);
}
do {
label = network.get(label).get(instructions[step++ % length]);
} while (!targetPattern.matcher(label).find());
return String.valueOf(step);
}
String part2() {
return null;
return part2("A$", "Z$");
}
String part2(String startRegex, String targetRegex) {
Pattern targetPattern = Pattern.compile(startRegex);
long ans = network.keySet().stream()
.filter(label -> targetPattern.matcher(label).find())
.map(label -> part1(label, targetRegex))
.mapToLong(Long::parseUnsignedLong)
.reduce(1, ArithmeticUtils::lcm);
return String.valueOf(ans);
}
static final class ElementMap extends HashMap<Character, String> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment