Advertisement
musifter

AoC 2023 day 9 part 1 (Perl)

Dec 9th, 2023 (edited)
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.52 KB | Source Code | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.26;
  4. use warnings;
  5.  
  6. use List::Util      qw(all sum);
  7.  
  8. sub chain (&@) {
  9.     my $block = shift;
  10.     return (map { &$block( $_[$_-1], $_[$_] ) } (1 .. $#_));
  11. }
  12.  
  13. my @input = map { [m/([-0-9]+)/g] } <>;
  14.  
  15. my $sum = 0;
  16. foreach my $list (@input) {
  17.     my @table = ($list);
  18.  
  19.     my $i = 0;
  20.     do {
  21.         $i++;
  22.         $table[$i] = [chain { $_[1] - $_[0] } $table[$i-1]->@*];
  23.     } until (all { $_ == 0 } $table[$i]->@*);
  24.  
  25.     $sum += sum map { $_->[-1] } @table;
  26. }
  27.  
  28. say "Part 1: $sum";
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement