From c1b6b94528efc48af820bda7c7557b95729c6af7 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sat, 11 Dec 2021 16:37:35 +0700 Subject: [aoc/2021] Finish day 11 The solution is pure but really, really slow. --- aoc/2021/11/part-one.raku | 12 ++++++++++++ aoc/2021/11/part-two.raku | 9 +++++++++ 2 files changed, 21 insertions(+) create mode 100644 aoc/2021/11/part-one.raku create mode 100644 aoc/2021/11/part-two.raku diff --git a/aoc/2021/11/part-one.raku b/aoc/2021/11/part-one.raku new file mode 100644 index 0000000..88cf4eb --- /dev/null +++ b/aoc/2021/11/part-one.raku @@ -0,0 +1,12 @@ +sub adj($i, $k) { [&&] map -2 < * < 2, [«-»] map { polymod $_: 10 }, ($i, $k) } +sub flash(@level, $count) { + my $i = first * > 9, @level, :k; + return ({ max $_, 0 } for @level), $count without $i; + flash ({ $i == $^k ?? -8 !! $^v + adj $i, $^k } for @level.kv), $count + 1 +} +sub step($times, @level, $count=0) { + return $count unless $times > 0; + step $times - 1, |flash @level »+» 1, $count +} +my $input = lines slurp 'input'; +put step 100, $input.join.comb diff --git a/aoc/2021/11/part-two.raku b/aoc/2021/11/part-two.raku new file mode 100644 index 0000000..a362b16 --- /dev/null +++ b/aoc/2021/11/part-two.raku @@ -0,0 +1,9 @@ +sub adj($i, $k) { [&&] map -2 < * < 2, [«-»] map { polymod $_: 10 }, ($i, $k) } +sub flash(@level) { + my $i = first * > 9, @level, :k; + return map { max $_, 0 }, @level without $i; + flash map { $i == $^k ?? -8 !! $^v + adj $i, $^k }, @level.kv +} +sub step(@level) { (0 == all @level) ?? 0 !! 1 + step flash @level »+» 1 } +my $input = lines slurp 'input'; +put step $input.join.comb -- cgit 1.4.1