#!/usr/bin/perl -w

use v5.38;
use List::Util qw(product);

my %types;
my %dests;
my %ffs;
my %inps;

while (<>) {
	my ($src, @dst) = /\w+/g;
	($types{$src}) = /^(\W)/;
	@{ $dests{$src} } = @dst;
	$inps{$_}->{$src} = 0 for @dst;
}

my @sums = (0, 0);

sub snd {
	my ($q, $name, $pulse) = @_;
	for (@{ $dests{$name} }) {
		push @$q, [ $_ , $pulse, $name ];
		$sums[$pulse]++;
	}
}

for (1 .. 1000) {
	my @q = ([ 'broadcaster', 0, 'button' ]);
	$sums[0]++;
	while (@q) {
		my $p = shift @q;
		my ($name, $pulse, $origin) = @$p;
		my $type = $types{$name} // '';

		if ($type eq '%') {
			if (!$pulse) {
				$ffs{$name} = !$ffs{$name};
				snd(\@q, $name, $ffs{$name});
			}
		} elsif ($type eq '&') {
			my $in = $inps{$name};
			$in->{$origin} = $pulse;
			my $out = 1;
			$out *= $_ for values %$in;
			snd(\@q, $name, !$out);
		} else {
			snd(\@q, $name, 0);
		}
	}
}

say "@sums -> ", product @sums;

