Skip to content

Instantly share code, notes, and snippets.

@turanegaku
Created December 19, 2016 15:59
Show Gist options
  • Select an option

  • Save turanegaku/61f6bf2dc83ba5cfe12c8d59c72d38f9 to your computer and use it in GitHub Desktop.

Select an option

Save turanegaku/61f6bf2dc83ba5cfe12c8d59c72d38f9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
S = 20
N = 5
# 初期状態
field = Array.new(S) { Array.new(S, false) }
field[5][8] = true
field[5][9] = true
field[5][10] = true
field[6][8] = true
field[6][10] = true
field[7][10] = true
field[8][7] = true
field[8][8] = true
field[8][9] = true
field[8][10] = true
field[8][11] = true
field[8][12] = true
field[8][13] = true
field[9][7] = true
field[9][10] = true
field[9][13] = true
field[10][7] = true
field[10][8] = true
field[10][9] = true
field[10][10] = true
field[10][11] = true
field[10][12] = true
field[10][13] = true
field[11][7] = true
field[11][10] = true
field[11][13] = true
field[12][7] = true
field[12][8] = true
field[12][9] = true
field[12][10] = true
field[12][11] = true
field[12][12] = true
field[12][13] = true
field[13][7] = true
field[13][13] = true
field[13][6] = true
field[13][12] = true
field[13][13] = true
# 表示関数
def printField(field)
for y in 0...S do
for x in 0...S do
print field[y][x] ? '*' : '.'
end
puts
end
puts
end
def prev(field, deep)
return field if deep == 0
File.open('./a%d.csp' % deep, 'w') do |io|
for y in 0...S do
for x in 0...S do
if x == 0 || y == 0 || x == S - 1 || y == S - 1
io.puts '(int cell%02d%02d 0)' % [y, x]
else
io.puts '(int cell%02d%02d 0 1)' % [y, x]
end
end
end
io.puts
for y in 1...S - 1 do
for x in 1...S - 1 do
l = []
for sy in y - 1..y + 1 do
for sx in x - 1..x + 1 do
l.push('cell%02d%02d' % [sy, sx]) unless sy == y && sx == x
end
end
if field[y][x]
io.puts(' (or (and (= cell%02d%02d 1) (= (+ %s) 2)) (= (+ %s) 3))' % [y, x, l.join(' '), l.join(' ')])
else
io.puts('(not (or (and (= cell%02d%02d 1) (= (+ %s) 2)) (= (+ %s) 3)))' % [y, x, l.join(' '), l.join(' ')])
end
end
end
io.puts
end
loop do
r = `sugar ./a#{deep}.csp`
return nil if r == "s UNSATISFIABLE\n"
field1 = Array.new(S) { Array.new(S, false) }
r.lines do|line|
if line[0] == 'a'
line.match(/cell(\d\d)(\d\d)\s*(\d)/) do |md|
field1[md[1].to_i][md[2].to_i] = !md[3].to_i.zero?
end
end
end
puts deep + 1
printField(field1)
pres = prev(field1, deep - 1)
return pres unless pres.nil?
File.open('./a%d.csp' % deep, 'a') do |io|
l = []
for y in 1...S - 1 do
next unless y == 3
for x in 1...S - 1 do
l.push('(= cell%02d%02d %d)' % [y, x, field1[y][x] ? '1' : '0'])
end
end
io.puts('(not (and %s))' % l.join(' '))
end
end
end
puts 0
printField(field)
field1 = prev(field, N)
exit if field1.nil?
puts 'done !!!!!!!'
puts '======================'
puts 'result here'
puts N
printField(field1)
for i in 0...N do
field2 = Array.new(S) { Array.new(S, false) }
for y in 1...S - 1 do
for x in 1...S - 1 do
c = 0
for sy in y - 1..y + 1 do
for sx in x - 1..x + 1 do
next if sx == x && sy == y
c += 1 if sx >= 0 && sy >= 0 && sx < S && sy < S && field1[sy][sx]
end
end
field2[y][x] = true if (field1[y][x] && c == 2) || c == 3
end
end
puts N - 1 - i
printField(field2)
field1 = field2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment