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;
- #
- # Mainline
- #
- my @input = map { chomp; $_ } <>;
- my $part2 = 0;
- # Create regex for finding first digit before a bigger digit
- my $regex = '(?:' . join( '|', map { "[1-@{[$_-1]}](?=$_)" } (2 .. 9) ) . ')';
- foreach my $queue (@input) {
- my @bank = split( //, substr( $queue, 0, -LEN, '' ) );
- while (my $jolts = pop @bank) {
- if ($jolts >= substr( $queue, 0, 1 )) {
- $queue = $jolts . $queue; # add jolts to front
- $queue =~ s#$regex##; # remove first digit followed by greater
- }
- }
- $part2 += substr( $queue, 0, LEN ); # truncate if needed
- }
- say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment