about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2021-12-11 16:37:35 +0700
committerNguyễn Gia Phong <mcsinyx@disroot.org>2021-12-11 16:45:16 +0700
commitc1b6b94528efc48af820bda7c7557b95729c6af7 (patch)
treebb8491f8e3acfb924cb0ce622a89a18dc6dd8b2c
parent9d041894e485f3dcd93a952266273a25f6614f07 (diff)
downloadcp-c1b6b94528efc48af820bda7c7557b95729c6af7.tar.gz
[aoc/2021] Finish day 11
The solution is pure but really, really slow.
-rw-r--r--aoc/2021/11/part-one.raku12
-rw-r--r--aoc/2021/11/part-two.raku9
2 files changed, 21 insertions, 0 deletions
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