]> www.fi.muni.cz Git - aoc.git/commitdiff
2024 Day 1: need to read the task more carefully
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sun, 1 Dec 2024 05:54:35 +0000 (06:54 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sun, 1 Dec 2024 05:54:35 +0000 (06:54 +0100)
2024/01.pl [new file with mode: 0755]
2024/02.pl [new file with mode: 0755]

diff --git a/2024/01.pl b/2024/01.pl
new file mode 100755 (executable)
index 0000000..aeb93cc
--- /dev/null
@@ -0,0 +1,20 @@
+#!/usr/bin/perl -w
+
+use v5.40;
+
+my (@l1, @l2);
+while (<>) {
+       my @f = /\d+/g;
+       push @l1, $f[0];
+       push @l2, $f[1];
+}
+@l1 = sort { $a <=> $b } @l1;
+@l2 = sort { $a <=> $b } @l2;
+
+my $sum;
+while (@l1) {
+       $sum += abs(shift(@l1) - shift(@l2));
+}
+
+say $sum;
+
diff --git a/2024/02.pl b/2024/02.pl
new file mode 100755 (executable)
index 0000000..99f0018
--- /dev/null
@@ -0,0 +1,16 @@
+#!/usr/bin/perl -w
+
+use v5.40;
+
+my (@l, %h);
+while (<>) {
+       my @f = /\d+/g;
+       push @l, $f[0];
+       $h{$f[1]}++;
+}
+
+my $sum;
+$sum += $_ * $h{$_} for @l;
+
+say $sum;
+