5 my ($rounds, $max, $cups) = @ARGV;
7 my @next_cup = (1 .. $max);
9 my $prev = @next_cup - 1;
11 for my $cup (split //, $cups) {
12 $cup--; # count cup numbers from 0, not from 1
13 $next_cup[$prev] = $cup;
15 $cur //= $cup; # The first one
21 my $cup = $next_cup[$cur];
22 my %num_seen = ($cur => 1);
26 $num_seen{ $cup } = 1;
27 # print $cup + 1, ' ';
28 $cup = $next_cup[$cup];
31 while ($num_seen{$dest}) {
33 $dest = $max-1 if $dest < 0;
35 # print " dest: ", $dest + 1, "\n";
36 $next_cup[$cur] = $cup;
37 my $end = $next_cup[$dest];
38 $next_cup[$dest] = $pickup[0];
39 $next_cup[$pickup[-1]] = $end;
41 $cur = $next_cup[$cur];
45 # $end = $next_cup[$end];
46 # } while ($end != $cur);
48 print "round $rounds\n" if $rounds % 100_000 == 0;
51 print "one -> ", $next_cup[0] + 1, '*', $next_cup[$next_cup[0]] + 1,
52 "=", ($next_cup[0]+1) * ($next_cup[$next_cup[0]]+1), "\n";