Skip to content

Instantly share code, notes, and snippets.

@mango314
Last active December 26, 2015 01:19
Show Gist options
  • Save mango314/7070974 to your computer and use it in GitHub Desktop.
Save mango314/7070974 to your computer and use it in GitHub Desktop.
script for making skyscraper puzzles

A script for generating random latin squares and Skyscraper puzzles.

  3 1 4 2 3 3 2
  -------------
2|             |2
3|             |4
1|             |3
2|             |2
3|             |3
4|             |3
4|             |1
  -------------
  1 4 2 3 2 2 2

Rules

Each row and each column contains the numbers 1 thru 7 in some order. They represent the heights of buildings... the two clues on each side indicate how many buildings you can see from each side.

see 3 bldgs --> 2 4 1 5 3 <-- see dos bldgs

Reglas

Igual a Sudoku, cada fila contiene los numeros 1-7 en alugn orden, representando alturas de edificios. Los numeros al lado indican cuandas edificios se ve en los dos direcciones. En este ejemplo, el 1 esta escondido en las dos direcciones

veo 3 edficios --> 2 4 1 5 3 <-- veo dos edificios

Ejemplo Example

  3 3 1 4 2
  ---------
2|         |3
1|         |2
4|         |1
3|         |2
2|         |3
  ---------
  3 2 2 1 2
import random
def perm(x):
for i in range(len(x)):
j = i + int((len(x) - i)*random.random())
c = x[j]
x[j] = x[i]
x[i] = c
return x
def latinSquare(L):
# slow for large values L > 8
a = []
count = [0]
while(len(a) < L):
p = perm(range(L))
b = [[a[x][y] for x in range(len(a))] + [p[y]] for y in range(L)]
count[-1] += 1
if sum([len(x) - len(list(set(x))) for x in b]) ==0:
a += [p]
count += [0]
if count[-1] > 1000000:
return a
return a
#sq = latinSquare(6)
#for x in sq:
# print x
def bldgs(x):
Max = -1
count = 0
for t in x:
if(t > Max):
Max = t
count += 1
return count
def puzzle(L):
sq = latinSquare(L)
N = [ bldgs([sq[y][x] for y in range(L)]) for x in range(L)]
S = [bldgs([sq[L-1-y][x] for y in range(L)])for x in range(L)]
E = [bldgs([sq[y][L-1-x] for x in range(L)]) for y in range(L)]
W = [bldgs([sq[y][x] for x in range(L)]) for y in range(L)]
puz = []
puz += [' ' + ' '.join([str(x) for x in N])]
puz += [' ' + (2*L-1)*'-']
for x in range(L):
puz += [("%s| "+(L-1)*" " + "|%s") %(E[x], W[x])]
#puz += [("%s|"+' '.join([str(y) for y in sq[x]]) + "|%s") %(W[x], E[x])]
puz += [' ' + (2*L-1)*'-']
puz += [' ' + ' '.join([str(x) for x in S])]
return '\n'.join(puz)
if __name__="__main__":
print puzzle(7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment