Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- my @ring;
- my ($curr, $prev);
- # Read in file, building ring
- chomp( my $input = <> );
- foreach (split( //, $input )) {
- $curr //= $_;
- $ring[$prev] = $_ if (defined $prev);
- $prev = $_;
- }
- $ring[$prev] = $curr;
- # run 100 turns
- foreach (1 .. 100) {
- # Advance ptr marking cups (cup 0 doesn't exist, marked as "grabbed" too)
- my $ptr = $curr;
- my %grab = (0 => 1, map { $ptr = $ring[$ptr]; $ptr => 1 } (1 .. 3));
- # Calculate destination (assuming cups 1-9 in input)
- my $dest = $curr - 1;
- $dest = ($dest - 1) % 10 while (exists $grab{$dest});
- # Relink ring
- ($ring[$curr], $ring[$ptr], $ring[$dest]) = ($ring[$ptr], $ring[$dest], $ring[$curr]);
- $curr = $ring[$curr];
- }
- print "Part 1: ";
- $curr = 1;
- print $curr while (($curr = $ring[$curr]) != 1);
- print "\n";
Add Comment
Please, Sign In to add comment