Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use v5.26;
- use warnings;
- use List::Util qw(all sum reduce);
- sub chain (&@) {
- my $block = shift;
- return (map { &$block( $_[$_-1], $_[$_] ) } (1 .. $#_));
- }
- my @input = map { [m#([-0-9]+)#g] } <>;
- my $part1 = 0;
- my $part2 = 0;
- foreach my $list (@input) {
- my @table = ($list);
- my $i = 0;
- do {
- $i++;
- $table[$i] = [chain { $_[1] - $_[0] } $table[$i-1]->@*];
- } until (all { $_ == 0 } $table[$i]->@*);
- $part1 += sum map { $_->[-1] } @table;
- $part2 += reduce { $b - $a } reverse map { $_->[0] } @table;
- }
- say "Part 1: $part1";
- say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement