-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.groovy
68 lines (56 loc) · 1.83 KB
/
day08.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import groovy.transform.TupleConstructor;
import groovy.transform.ToString;
@TupleConstructor
@ToString
class Node {
String name;
String left;
String right;
}
long lcm(long a, long b) {
return (a * b) / gcf(a, b);
}
long gcf(long a, long b) {
if (b == 0) {
return a;
} else {
return (gcf(b, a % b));
}
}
def input = new File("input/day08.txt").readLines()
def instructions = input[0].split("");
def network = input[2..-1].collect { line ->
def matcher = line =~ /^([A-Z0-9]{3}) = \(([A-Z0-9]{3}), ([A-Z0-9]{3})\)$/
def x = new Node(matcher[0][1], matcher[0][2], matcher[0][3]);
return x;
}.collectEntries {
[it.name, it]
}
def countSteps(instructions, network, startingNodes) {
def currentNodes = startingNodes;
def currentInstructionIndex = 0;
def stepsCount = 0;
def stepsToZ = [];
while (currentNodes.size() > 0) {
def instruction = instructions[currentInstructionIndex];
switch(instruction) {
case "L":
currentNodes = currentNodes.collect { node -> network[network[node.name].left] };
break
case "R":
currentNodes = currentNodes.collect { node -> network[network[node.name].right] };
break
default:
assert false : "Invalid instruction";
}
currentInstructionIndex = (currentInstructionIndex + 1) % instructions.size();
stepsCount++;
currentNodes.findAll { it.name[-1] == "Z" }.each {
stepsToZ << (stepsCount)
};
currentNodes = currentNodes.findAll { it.name[-1] != "Z" };
}
return stepsToZ.inject(1) { acc, it -> lcm(acc, it) };
}
println countSteps(instructions, network, [network["AAA"]]);
println countSteps(instructions, network, network.findAll { it.value.name[-1] == "A" }.values());