From 5a67962b6c752ed3064a2219bdb260c27a206cea Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sat, 11 Dec 2021 06:37:07 +0100 Subject: [PATCH] Day 11: too much coding --- 21.pl | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 22.pl | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100755 21.pl create mode 100755 22.pl diff --git a/21.pl b/21.pl new file mode 100755 index 0000000..8bbba21 --- /dev/null +++ b/21.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl -w + +use v5.16; + +my @m = map { chomp; [ split // ] } <>; + +my $sum; +for (1 .. 100) { + my %f; + my @q; + for my $x (0 .. 9) { + for my $y (0 .. 9) { + next if $f{$x,$y}; + next if ++$m[$y][$x] < 10; + $f{$x,$y} = 1; + push @q, [$x,$y]; + say "flashed:$x,$y"; + } } + while (@q) { + my $p = shift @q; + my ($x2, $y2) = @$p; + + for my $dx (-1 .. 1) { + for my $dy (-1 .. 1) { + next if $dx == 0 && $dy == 0; + next if $x2+$dx < 0 || $x2+$dx > 9; + next if $y2+$dy < 0 || $y2+$dy > 9; + next if $f{$x2+$dx,$y2+$dy}; + say $x2+$dx, ',', $y2+$dy, ' >',$m[$y2+$dy][$x2+$dx]; + next if ++$m[$y2+$dy][$x2+$dx] < 10; + $f{$x2+$dx,$y2+$dy} = 1; + push @q, [$x2+$dx, $y2+$dy]; + say "flashed ", $x2+$dx,',',$y2+$dy; + } } + } + for my $p (keys %f) { + my ($x, $y) = split /$;/, $p; + $m[$y][$x] = 0; + } + say "Step $_ flashes ", scalar keys %f; + say join("\n", map { join('', @$_) } @m); + say; + # last if $_ > 1; + $sum += keys %f; +} + +say $sum; + diff --git a/22.pl b/22.pl new file mode 100755 index 0000000..221c686 --- /dev/null +++ b/22.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl -w + +use v5.16; + +my @m = map { chomp; [ split // ] } <>; + +my $sum; +my $step = 0; +while (++$step) { + my %f; + my @q; + for my $x (0 .. 9) { + for my $y (0 .. 9) { + next if $f{$x,$y}; + next if ++$m[$y][$x] < 10; + $f{$x,$y} = 1; + push @q, [$x,$y]; + say "flashed:$x,$y"; + } } + while (@q) { + my $p = shift @q; + my ($x2, $y2) = @$p; + + for my $dx (-1 .. 1) { + for my $dy (-1 .. 1) { + next if $dx == 0 && $dy == 0; + next if $x2+$dx < 0 || $x2+$dx > 9; + next if $y2+$dy < 0 || $y2+$dy > 9; + next if $f{$x2+$dx,$y2+$dy}; + say $x2+$dx, ',', $y2+$dy, ' >',$m[$y2+$dy][$x2+$dx]; + next if ++$m[$y2+$dy][$x2+$dx] < 10; + $f{$x2+$dx,$y2+$dy} = 1; + push @q, [$x2+$dx, $y2+$dy]; + say "flashed ", $x2+$dx,',',$y2+$dy; + } } + } + for my $p (keys %f) { + my ($x, $y) = split /$;/, $p; + $m[$y][$x] = 0; + } + say "Step $_ flashes ", scalar keys %f; + say join("\n", map { join('', @$_) } @m); + say; + last if keys %f == 100; +} + +say $step; + -- 2.43.5