-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.pl
72 lines (64 loc) · 1.58 KB
/
20.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
72
#!/usr/bin/perl -w
use strict;
no warnings 'portable';
use feature 'say';
use List::Util qw/sum min max reduce any all none notall first product uniq pairs mesh zip/;
use Math::Utils qw(:utility); # Useful functions
BEGIN {push @INC, "../lib";}
use AOC ':all';
$AOC::DEBUG_ENABLED=1;
$|=1;
my %types;
my %outputs;
my %memory;
my %interest;
my %result;
while (<>) {
chomp;
last unless $_;
my ($type,$mod,$to) = m{(.)(\S+) -> (.+)$};
$types{$mod} = $type;
$outputs{$mod} = [split(', ',$to)];
$interest{$mod}++ if ($type eq '&')
}
while (my ($k,$v) = each %outputs) {
for my $n (@$v) {
next unless (($types{$n}//'') eq '&');
$memory{$n}{$k}=0;
}
}
my @counts;
BIGLOOP: for (my $i=1; ; $i++) {
dbg "iteration $i";
my @q=([0,'roadcaster','button']);
while (@q) {
my ($sig,$node,$prev) = @{shift @q};
$counts[$sig]++;
my $type = $types{$node} // '';
dbg (($types{$prev}//'')."$prev -".($sig?'high':'low')."-> $type$node");
next unless $type;
if ($type eq '%') {
next if $sig;
$sig=$memory{$node}=!$memory{$node};
} elsif ($type eq '&') {
$memory{$node}{$prev} = $sig;
$sig = (notall {$_} (values(%{$memory{$node}})));
if ($sig && $interest{$node}) {
dbg("$node happy at $i");
$result{$node} = $i;
delete $interest{$node};
if (!%interest && ($i > 1000)) {
out(lcm(values(%result)));
last BIGLOOP;
}
}
}
for my $n (@{$outputs{$node}}) {
push @q, [$sig, $n, $node];
}
}
if ($i == 1000) {
dbg(\@counts);
out(product(@counts));
}
}