Day 8 implementation in Java
parent
1d96e28b33
commit
cfb6d09af2
|
|
@ -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<String> inputData = List.ofAll(reader.readContents());
|
||||||
|
|
||||||
|
List<Direction> instructions = List.ofAll(inputData.get(0).toCharArray())
|
||||||
|
.map(character -> character == 'L' ? Direction.LEFT : Direction.RIGHT);
|
||||||
|
|
||||||
|
Map<String, Tuple2<String, String>> 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<Long> 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<Direction> instructions,
|
||||||
|
Map<String, Tuple2<String, String>> graph,
|
||||||
|
Function<String, Boolean> conditionChecker
|
||||||
|
) {
|
||||||
|
Long count = 0L;
|
||||||
|
Iterator<Direction> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
package util;
|
||||||
|
|
||||||
|
import io.vavr.collection.List;
|
||||||
|
|
||||||
|
public class MathUtils {
|
||||||
|
public static Long leastCommonMultiple(List<Long> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue