--- /dev/null
+#!/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;
--- /dev/null
+#!/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;