From 735c1cc915c215e9d9e47c3e4c470ef4abb7326e Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sun, 1 Dec 2024 06:54:35 +0100 Subject: [PATCH] 2024 Day 1: need to read the task more carefully --- 2024/01.pl | 20 ++++++++++++++++++++ 2024/02.pl | 16 ++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100755 2024/01.pl create mode 100755 2024/02.pl diff --git a/2024/01.pl b/2024/01.pl new file mode 100755 index 0000000..aeb93cc --- /dev/null +++ b/2024/01.pl @@ -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 index 0000000..99f0018 --- /dev/null +++ b/2024/02.pl @@ -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; + -- 2.43.5