Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- # Change this to 2 for part 1
- use constant LEN => 12;
- sub chain_index (&@) {
- my $block = shift;
- my $ret = -1;
- foreach my $idx (0 .. $#_ - 1) {
- if (&$block( $_[$idx], $_[$idx+1] )) {
- $ret = $idx;
- last;
- }
- }
- return ($ret);
- }
- #
- # Mainline
- #
- my @input = map { chomp; [split //] } <>;
- my $part2 = 0;
- foreach my $bank (@input) {
- my @queue = splice( @$bank, -LEN );
- while (my $jolts = pop @$bank) {
- if ($jolts >= $queue[0]) {
- splice( @queue, (chain_index {$_[0] < $_[1]} @queue), 1 );
- unshift( @queue, $jolts );
- }
- }
- $part2 += join( '', @queue );
- }
- say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment