use 5.014; use warnings; our(\$R, \$C, \$M) = (14, 14, 7); if (@ARGV) { 3 == @ARGV or die "Usage: lights R C M"; (\$R, \$C, \$M) = map int, @ARGV; } my @c = ((\$C) x (\$R-1), \$C-\$M); my @m = map { (1<<\$_)-1 } @c; T: for my \$t (0 .. \$m[0]) { my @b; my @l = @m; for my \$r (0 .. \$R-1) { my \$b = \$b[\$r] = \$r <= 0 ? \$t : \$m[\$r] & \$l[\$r-1]; \$r+1 < \$R and \$l[\$r+1] ^= \$m[\$r+1] & \$b; \$l[\$r] ^= \$m[\$r] & (\$b<<1 ^ \$b ^ \$b>>1); 0 <= \$r-1 and \$l[\$r-1] ^= \$m[\$r-1] & \$b; } 0 == \$l[\$R-1] && 0 == \$l[\$R-2] or next T; say "Found solution: ["; for my \$r (0 .. \$R-1) { for my \$c (0 .. \$C-1) { print " ", (\$c[\$r] <= \$c ? " " : \$b[\$r]>>\$c & 1 ? "P" : "."); } if (0) { print " > "; for my \$c (0 .. \$C-1) { print " ", (\$c[\$r] <= \$c ? " " : \$l[\$r]>>\$c & 1 ? "*" : "."); } } say ""; } say "]"; } say "done searching (R=\$R, C=\$C, M=\$M)."; __END__ ##```## Found solution: [ . P . P . . . P . P . . . . . . P . . P . . P . . P P P P P . P . . . P . P . P . P P P . . . . . . . . P P . P . . . . P P P . P P P . P P . . P . P . P P P . . . P . P . . P P . . . . P . . P P . . P P . . . . P . . . . P . . P . P P P . . P P P P P P . P . P . P . P P . . . . . P P P P . P P P . P . . . P P P . . P . . . . . . P P P P . P P . P . . P . . P P . . . P P . . ] done searching (R=14, C=14, M=7). ```