]> www.fi.muni.cz Git - aoc.git/blob - 2020/46.pl
Day 25: examining the input
[aoc.git] / 2020 / 46.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 my ($rounds, $max, $cups) = @ARGV;
6
7 my @next_cup = (1 .. $max);
8 my $i = 0;
9 my $prev = @next_cup - 1;
10 my $cur;
11 for my $cup (split //, $cups) {
12         $cup--; # count cup numbers from 0, not from 1
13         $next_cup[$prev] = $cup;
14         $prev = $cup;
15         $cur //= $cup; # The first one
16 }
17 # $next_cup[6] = 2;
18
19 while ($rounds--) {
20         my @pickup;
21         my $cup = $next_cup[$cur];
22         my %num_seen = ($cur => 1);
23         # print "pickup: ";
24         for (1..3) {
25                 push @pickup, $cup;
26                 $num_seen{ $cup } = 1;
27                 # print $cup + 1, ' ';
28                 $cup = $next_cup[$cup];
29         }
30         my $dest = $cur;
31         while ($num_seen{$dest}) {
32                 $dest--;
33                 $dest = $max-1 if $dest < 0;
34         }
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;
40
41         $cur = $next_cup[$cur];
42         $end = $cur;
43         # do {
44         #       print $end+1, ',';
45         #       $end = $next_cup[$end];
46         # } while ($end != $cur);
47         # print "\n";
48         print "round $rounds\n" if $rounds % 100_000 == 0;
49 }
50
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";