diff --git a/java/src/main/java/day8/Main.java b/java/src/main/java/day8/Main.java new file mode 100644 index 0000000..4a35594 --- /dev/null +++ b/java/src/main/java/day8/Main.java @@ -0,0 +1,80 @@ +package day8; + +import io.vavr.Tuple; +import io.vavr.Tuple2; +import io.vavr.collection.Iterator; +import io.vavr.collection.List; +import io.vavr.collection.Map; +import util.FileReader; +import util.MathUtils; + +import java.util.function.Function; + +public class Main { + enum Direction { + LEFT, + RIGHT + } + + private static final String STARTING_NODE = "AAA"; + private static final String ENDING_NODE = "ZZZ"; + private static final String STARTING_NODES_SUFFIX = "A"; + private static final String ENDING_NODES_SUFFIX = "Z"; + + public static void main(String[] args) { + String inputFilePath = System.getProperty("user.dir") + "\\java\\src\\main\\java\\day8\\input.txt"; + FileReader reader = new FileReader(inputFilePath); + List inputData = List.ofAll(reader.readContents()); + + List instructions = List.ofAll(inputData.get(0).toCharArray()) + .map(character -> character == 'L' ? Direction.LEFT : Direction.RIGHT); + + Map> graph = inputData.removeAt(0) + .filter(string -> !string.isEmpty()) + .map(string -> string + .replaceAll(" ", "") + .replaceAll("\\(", "") + .replaceAll("\\)", "") + .replaceAll("=", ",") + .split(",")) + .map(List::of) + .toMap( + nodeIds -> nodeIds.get(0), + nodeIds -> Tuple.of(nodeIds.get(1), nodeIds.get(2)) + ); + + System.out.println(countIterationsUntilConditionIsMet(STARTING_NODE, instructions, graph, ENDING_NODE::equals)); + + List iterationCounts = graph.keySet().toList() + .filter(string -> string.endsWith(STARTING_NODES_SUFFIX)) + .map(node -> countIterationsUntilConditionIsMet( + node, + instructions, + graph, + n -> n.endsWith(ENDING_NODES_SUFFIX) + )); + + System.out.println(MathUtils.leastCommonMultiple(iterationCounts)); + } + + private static Long countIterationsUntilConditionIsMet( + String node, + List instructions, + Map> graph, + Function conditionChecker + ) { + Long count = 0L; + Iterator iterator = instructions.iterator(); + while (!conditionChecker.apply(node)) { + if (!iterator.hasNext()) { + iterator = instructions.iterator(); + } + Direction direction = iterator.next(); + node = direction == Direction.LEFT + ? graph.get(node).getOrElseThrow(RuntimeException::new)._1() + : graph.get(node).getOrElseThrow(RuntimeException::new)._2(); + count++; + } + return count; + } +} \ No newline at end of file diff --git a/java/src/main/java/util/MathUtils.java b/java/src/main/java/util/MathUtils.java new file mode 100644 index 0000000..d2d9cdc --- /dev/null +++ b/java/src/main/java/util/MathUtils.java @@ -0,0 +1,18 @@ +package util; + +import io.vavr.collection.List; + +public class MathUtils { + public static Long leastCommonMultiple(List numbers) { + return numbers + .foldLeft(1L, (x, y) -> x * y / greaterCommonDivisor(x, y) + ); + } + + public static Long greaterCommonDivisor(Long number, Long other) { + if (other == 0) { + return number; + } + return greaterCommonDivisor(other,number % other); + } +}