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);
- sub chain (&@) {
- my $block = shift;
- return (map { &$block( $_[$_-1], $_[$_] ) } (1 .. $#_));
- }
- my @input = map { [m/([-0-9]+)/g] } <>;
- my $sum = 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]->@*);
- $sum += sum map { $_->[-1] } @table;
- }
- say "Part 1: $sum";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement