From 3e6a252f664e43d0a34805d37e072c9be2c7af8e Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Tue, 7 Dec 2021 15:34:08 +0700 Subject: [aoc/2021] Add day 7 --- aoc/2021/07/part-one.raku | 3 +++ aoc/2021/07/part-two.raku | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 aoc/2021/07/part-one.raku create mode 100644 aoc/2021/07/part-two.raku diff --git a/aoc/2021/07/part-one.raku b/aoc/2021/07/part-one.raku new file mode 100644 index 0000000..81cb7d4 --- /dev/null +++ b/aoc/2021/07/part-one.raku @@ -0,0 +1,3 @@ +my @input = map +*, split ',', trim slurp 'input'; +sub cost($align) { [+] map { abs $align - $_ }, @input } +put min (cost $_ for (min @input)...(max @input)) diff --git a/aoc/2021/07/part-two.raku b/aoc/2021/07/part-two.raku new file mode 100644 index 0000000..d4934d9 --- /dev/null +++ b/aoc/2021/07/part-two.raku @@ -0,0 +1,4 @@ +my &cum = { $_ * ($_ + 1) div 2 }; +my @input = map +*, split ',', trim slurp 'input'; +sub cost($align) { [+] map { cum abs $align - $_ }, @input } +put min (cost $_ for (min @input)...(max @input)) -- cgit 1.4.1