--- /dev/null
+#!/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;
+}
+
+