about summary refs log tree commit diff
diff options
context:
space:
mode:
-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