From 3a947de19792c929af95884d9b1c66135d28b687 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Tue, 7 Dec 2021 06:25:25 +0100 Subject: [PATCH] Day 7: My brain was too sluggish today. --- 13.pl | 18 ++++++++++++++++++ 14.pl | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100755 13.pl create mode 100755 14.pl diff --git a/13.pl b/13.pl new file mode 100755 index 0000000..79740a3 --- /dev/null +++ b/13.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl -w + +use v5.16; + +my @c = split /[,\s]/, <>; + +my $max; +($max = !$max || $max < $_ ? $_ : $max) for @c; + +my $min_f; +for my $pos (0 .. $max) { + my $f = 0; + $f += abs($_ - $pos) for @c; + $min_f = $f if !$min_f || $min_f > $f; + # say "$pos $f $min_f"; +} + +say $min_f; diff --git a/14.pl b/14.pl new file mode 100755 index 0000000..3822e47 --- /dev/null +++ b/14.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl -w + +use v5.16; + +my @c = split /[,\s]/, <>; + +my $max; +($max = !$max || $max < $_ ? $_ : $max) for @c; + +my $min_f; +for my $pos (0 .. $max) { + my $f = 0; + for my $c1 (@c) { + my $dist = abs($c1 - $pos); + $f += $dist * ($dist+1) /2; + } + $min_f = $f if !$min_f || $min_f > $f; + # say "$pos $f $min_f"; +} + +say $min_f; -- 2.43.5