Skip to content

Instantly share code, notes, and snippets.

@thedeemon
Created March 5, 2021 23:14
Show Gist options
  • Save thedeemon/a62eca3823645b09d014aa5bab0a0386 to your computer and use it in GitHub Desktop.
Save thedeemon/a62eca3823645b09d014aa5bab0a0386 to your computer and use it in GitHub Desktop.
L=11
@field = Array.new(32*32) {0}
@vecs = [[1,0], [0,-1], [-1,0], [0,1]]
def fld(x,y) @field[y*32+x] end
def set(x,y,v) @field[y*32+x] = v end
def step(x, y, ang)
vx,vy = @vecs[ang & 3]
return x+vx, y+vy
end
@dna = Array.new(L)
@path = Array.new(L) {[16,16]}
def cpn
n = 0
for p in @path do
if fld(p[0], p[1])==1 then
n += 1 if fld(p[0]+1, p[1])==1
n += 1 if fld(p[0], p[1]+1)==1
end
end
n
end
def draw(n, x,y, angle)
return if fld(x,y) != 0
set(x,y, @dna[n])
@path[n][0] = x; @path[n][1] = y
if n == L-1 then
cp = cpn()
@maxcpn = cp if cp > @maxcpn
set(x,y, 0)
return
end
nx,ny = step(x,y, angle)
draw(n+1, nx,ny, angle)
nx,ny = step(x,y, angle-1)
draw(n+1, nx,ny, (angle-1)&3)
nx,ny = step(x,y, angle+1)
draw(n+1, nx,ny, (angle+1)&3)
set(x,y, 0)
end
def optLen(seed)
for i in (0...L) do
@dna[i] = if (seed & (1 << i)) > 0 then 2 else 1 end
end
set(16,16, @dna[0])
@maxcpn = 0
draw(1, 17,16, 0)
@maxcpn
end
puts (0...(1<<L)).map{|i| optLen(i)}.reduce(:+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment