X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=21.pl;fp=21.pl;h=4a3d0ba658d15f1f547294a7a36bab7d9f81a834;hb=43e4d5ca179c9b228a3eadc780d38c341824cc37;hp=0000000000000000000000000000000000000000;hpb=26c4c3c6792bf77be7b33a4f892a72c7f23c45e1;p=aoc2020.git diff --git a/21.pl b/21.pl new file mode 100755 index 0000000..4a3d0ba --- /dev/null +++ b/21.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl -w + +use strict; + +my @seats = map { chomp; [ split // ] } (<>); + +my $cols = @{ $seats[0] }; +my $rows = @seats; + +print "$cols x $rows\n"; + +while (1) { + my $was_change = 0; + my $occup = 0; + my @newseats; + for my $row (0 .. $rows-1) { + my @newrow; + for my $col (0 .. $cols-1) { + my $neigh = ''; + for my $add ([-1, -1], [-1, 0], [-1, 1], + [0, -1], [0, 1], + [1, -1], [1, 0], [1, 1]) { + my $row1 = $row + $add->[0]; + my $col1 = $col + $add->[1]; + next if $row1 >= $rows || $row1 < 0 + || $col1 >= $cols || $col1 < 0; + $neigh .= $seats[$row1]->[$col1]; + } + my $neigh_empty =()= $neigh =~ /L/g; + my $neigh_occup =()= $neigh =~ /#/g; + + if ($seats[$row]->[$col] eq 'L' && !$neigh_occup) { + push @newrow, '#'; + $was_change = 1; + $occup++; + } elsif ($seats[$row]->[$col] eq '#' && $neigh_occup >= 4) { + push @newrow, 'L'; + $was_change = 1; + } else { + push @newrow, $seats[$row]->[$col]; + $occup++ if $seats[$row]->[$col] eq '#'; + } + } + push @newseats, \@newrow; + } + @seats = @newseats; + #for my $row (@seats) { + # print @$row, "\n"; + #} + print "$occup occupied seats\n"; + # print "\n"; + last if !$was_change; +} + +