| Bytes | Lang | Time | Link |
|---|---|---|---|
| 447 | Python3 | 250330T170554Z | Ajax1234 |
| 102 | Ruby | 110814T135153Z | migimaru |
| 133 | Ruby | 110814T082950Z | Howard |
| nan | 110813T193628Z | Ming-Tan |
Python3, 447 bytes
E=enumerate
def U(I,x,y):I[(x,y)]=int(not I[(x,y)])
def f(b):
d={(x,y):v for x,r in E(b)for y,v in E(r)}
p,s=[(d,[[0 for _ in i]for i in b])],[d]
for i,m in p:
if sum(i.values())==0:return m
C=[]
for x,y in i:
r=0
I={**i};r+=I[(x,y)];U(I,x,y);M=eval(str(m));M[x][y]=1
for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]:
if(N:=(x+X,y+Y))in d:r+=I[N];U(I,*N)
if I not in s:C+=[(I,M,r)]
I,M,_=max(C,key=lambda x:x[-1]);p+=[(I,M)];s+=[I]
Ruby (130) (121) (113) (102)
EDIT: Saved 9 characters, thanks to Howard.
EDIT 2: Another 8 characters, courtesy of Howard.
EDIT 3: Saved 11 more characters after learning how to use Ruby's % operator for sprintf.
b=(0..4).map{gets.to_i 2}
32.times{|x|z=0;k=b.map{|i|x=z^i^31&(x<<1^x>>1)^z=x;'%05b'%z}
puts k if x<1}
Accepts 5 lines of input from stdin. Outputs all solutions as 5x5 grids as specified, though there are no extra line breaks between solutions (or between the input and the solutions).
Solutions are found by iterating through all 32 possibilities for the top row, since the top row determines the switches that must be flipped in the following rows.
Input:
00000
00000
00110
01000
00101
Output:
00000
00000
00000
00110
00001
01110
10101
11011
10011
01111
10101
10101
00000
10011
10100
11011
00000
11011
00110
11010
Ruby, 133 characters
t=->z,v,s{v<0?z&33814345664<1&&s :t[z,v-1,s+?0]||t[z^4321<<(v+v/5),v-1,s+?1]}
puts t[32*$<.read.tr(?\n,?0).to_i(2),24,''].scan /.{5}/
This is a brute-force search through all possible on/off combinations. The input must be specified on stdin with exactly five lines of five 0/1 characters each and a newline after each one, e.g.:
> ruby lights.rb
11111
11111
11011
11111
11111
^Z
00000
11111
10001
11111
00000
F#
brute-force, will golf later
open System;
let a = Array2D.create 5 5 false
let b = Array2D.create 5 5 false
for i = 0 to 4 do
for j = 0 to 4 do
a.[i, j] <- if Console.Read() = 49 then true else false
Console.ReadLine() |> ignore
let r = Random()
while Seq.cast a |> Seq.exists id do
let x, y = r.Next(5), r.Next(5)
b.[x, y] <- not b.[x, y]
for u, v in [ x, y;
x - 1, y;
x + 1, y;
x, y - 1;
x, y + 1 ] do
try
a.[u, v] <- not a.[u, v]
with
| _ -> ()
printfn "%s" (let mutable z = ""
for i = 0 to 4 do
for j = 0 to 4 do
z <- z + (if b.[i, j] then "1" else "0")
z <- z + "\n"
z)