From c5060bdbc898139cea04423a11650446d3a291a3 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sat, 9 Dec 2023 06:27:27 +0100 Subject: [PATCH] Day 9: interesting task --- 2023/17.pl | 20 ++++++++++++++++++++ 2023/18.pl | 24 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100755 2023/17.pl create mode 100755 2023/18.pl diff --git a/2023/17.pl b/2023/17.pl new file mode 100755 index 0000000..ab77a83 --- /dev/null +++ b/2023/17.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl -w + +use v5.38; +use List::Util qw(sum); + +my $sum; +while (<>) { + my @seq = /-?\d+/g; + my @rest; + while (grep $_, @seq) { + for my $i (0 .. $#seq-1) { + $seq[$i] = $seq[$i+1]-$seq[$i]; + } + push @rest, pop @seq; + } + $sum += sum @rest; +} + +say $sum; + diff --git a/2023/18.pl b/2023/18.pl new file mode 100755 index 0000000..f41b7b1 --- /dev/null +++ b/2023/18.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl -w + +use v5.38; + +my $sum; +while (<>) { + my @seq = /-?\d+/g; + my @rest; + while (grep $_, @seq) { + push @rest, $seq[0]; + for my $i (0 .. $#seq-1) { + $seq[$i] = $seq[$i+1]-$seq[$i]; + } + pop @seq; + } + my $s = 0; + for (reverse @rest) { + $s = $_ - $s; + } + $sum += $s; +} + +say $sum; + -- 2.43.5