Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- use List::Util qw(reduce);
- use constant LEN => 12;
- my @input = map { chomp; [split //] } <>;
- my $part2 = 0;
- foreach my $bank (@input) {
- my @queue = splice( @$bank, -LEN );
- BATTERY:
- for (my $i = $bank->$#*; $i >= 0; $i--) {
- my $jolts = $bank->[$i];
- # Cascade jolts down the queue:
- for (my $j = 0; $j < LEN; $j++) {
- next BATTERY if ($jolts < $queue[$j]);
- ($queue[$j], $jolts) = ($jolts, $queue[$j]);
- }
- }
- $part2 += reduce { 10 * $a + $b } @queue;
- }
- say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment