-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-8-star-2.pl
executable file
·71 lines (58 loc) · 1.4 KB
/
day-8-star-2.pl
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
69
70
71
#!/usr/bin/perl
use warnings;
use 5.036;
chomp(my @lines = <>);
my @steps;
my %nodes;
foreach my $line (@lines) {
next if $line eq '';
if ($#steps < 0) {
@steps = split(//, $line);
# say "@steps";
next;
}
my @tmp = $line =~ /(\w{3}) = \((\w{3})\, (\w{3})\)/;
$nodes{$tmp[0]} = { 'L' => $tmp[1], 'R' => $tmp[2] };
}
my $i = 0;
my @currentNodes = grep { /A\z/ } keys %nodes;
say "@currentNodes";
# look for loops instead
# and calculate the least common multiple
my @loopLengths;
foreach my $cn (@currentNodes) {
my $currentNode = $cn;
my $i = 0;
my $previousDiff = 0;
my $diff = 0;
my $lastI = 0;
# say $currentNode;
while (1) {
my $stepNumber = $i % ($#steps + 1);
my $step = $steps[$stepNumber];
my $nextNode = $nodes{$currentNode}->{$step};
# say "Current: $cn, next: $nextNode";
$currentNode = $nextNode;
$i++;
if ($currentNode =~ /Z\z/) {
$previousDiff = $diff;
$diff = $i - $lastI;
last if $previousDiff == $diff;
$lastI = $i;
push(@loopLengths, $diff);
# say $diff;
}
}
}
sub gcd($x, $y) {
while ($x) { ($x, $y) = ($y % $x, $x) }
return $y;
}
sub lcm($x, $y) {
return ((($x && $y) and $x / &gcd($x, $y) * $y) or 0);
}
my $x = 1;
foreach my $ll (@loopLengths) {
$x = lcm($x, $ll);
}
say $x;