From 91e0bf0f4c2b28b6a70c5770262f3972b6879857 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Tue, 7 Dec 2021 11:15:45 -0500 Subject: Solve day 7 part 2 --- 2021/day07.pl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/2021/day07.pl b/2021/day07.pl index 209bcb2..a403125 100644 --- a/2021/day07.pl +++ b/2021/day07.pl @@ -1,7 +1,18 @@ use strict; use warnings; +use List::Util qw(min max sum); my @crabs = sort { $a <=> $b } split(/,/, <>); my $median = $crabs[@crabs / 2]; -my $cost = 0; -$cost += abs($_ - $median) for @crabs; +my $cost = sum(map { abs($_ - $median) } @crabs); print "$cost\n"; +sub cost { + my ($to, $from) = @_; + my $n = abs($from - $to); + $n * ($n + 1) / 2; +} +sub totalCost { + my $x = shift; + sum(map { cost($x, $_) } @_); +} +my $min = min(map { totalCost($_, @crabs) } min(@crabs)..max(@crabs)); +print "$min\n"; -- cgit 1.4.1 lass='sub right'>
summary refs log tree commit diff
path: root/unscoop.c (unfollow)
Commit message (Expand)Author
2020-01-20Use Xr for first "litterbox" in README 1.0June McEnroe
2020-01-20Introduce pounce link in READMEJune McEnroe
2020-01-18Use < for beforeJune McEnroe
2020-01-18Rearrange code in scoopJune McEnroe
2020-01-16Add scoop flag for local timeJune McEnroe
2020-01-14Remove note about usernames for pounceJune McEnroe
2020-01-14Reference IRCv3.1 SASL specJune McEnroe
2020-01-14Ensure ansi resets formatting and null-terminatesJune McEnroe
2020-01-14Implement IRC formatting to ANSI translationJune McEnroe
2020-01-14Add -b flag for live backupJune McEnroe
2020-01-14Add columnsize = 0 option to FTS indexJune McEnroe
2020-01-12Add option for client cert and SASL EXTERNALJune McEnroe
2020-01-12Add Linux.mkJune McEnroe
2020-01-11Fix unscoop dedup window syntaxJune McEnroe
2020-01-11Bump busy timeout to 10sJune McEnroe