Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- # Table where letters are in priority positions
- my $table = join( '', @{['^', 'a' .. 'z', 'A' .. 'Z']} );
- my $part1 = 0;
- my $part2 = 0;
- my $badge = '';
- while (<>) {
- chomp;
- # Part 1
- # Insert sentinel in middle, sum if match on both sides.
- substr( $_, length() / 2, 0, '#' );
- $part1 += index( $table, $1 ) if (m/(\w).*#.*\1/);
- # Part 2
- # Append lines with sentinel between them.
- $badge .= "$_#";
- # When a match occurs across three, sum and empty badge.
- if ($badge =~ m/(\w).*#.*\1.*#.*\1/) {
- $part2 += index( $table, $1 );
- $badge = '';
- }
- }
- print "Part 1: $part1\n";
- print "Part 2: $part2\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement