Advertisement
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(sum zip);
- my (@list1, @list2, %counts);
- while (<>) {
- m#(\d+)\s+(\d+)#;
- push( @list1, $1 );
- push( @list2, $2 );
- $counts{$2}++;
- }
- @list1 = sort {$a <=> $b} @list1;
- @list2 = sort {$a <=> $b} @list2;
- say "Part 1: ", sum map { abs($_->[0] - $_->[1]) } zip \@list1, \@list2;
- say "Part 2: ", sum map { $_ * ($counts{$_} // 0) } @list1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement