]> www.fi.muni.cz Git - aoc2021.git/blob - 14.pl
Day 25: pretty straightforward
[aoc2021.git] / 14.pl
1 #!/usr/bin/perl -w
2
3 use v5.16;
4
5 my @c = split /[,\s]/, <>;
6
7 my $max;
8 ($max = !$max || $max < $_ ? $_ : $max) for @c;
9
10 my $min_f;
11 for my $pos (0 .. $max) {
12         my $f = 0;
13         for my $c1 (@c) {
14                 my $dist = abs($c1 - $pos);
15                 $f += $dist * ($dist+1) /2;
16         }
17         $min_f = $f if !$min_f || $min_f > $f;
18         # say "$pos $f $min_f";
19 }
20
21 say $min_f;