Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python -O
- from __future__ import unicode_literals
- import sys
- import random, array
- import itertools as it
- import operator as op
- def powerset(s):
- "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
- return it.chain.from_iterable(it.combinations(s, r) for r in xrange(len(s)+1))
- def value(desc):
- return (68, 64, 4, 160, 128, 32, 0)[
- ('VERTICAL', 'UP', 'DOWN', 'HORIZONTAL', 'LEFT', 'RIGHT', None).index(desc)]
- OTHERS= [reduce(op.or_, x, 0) for x in powerset((16, 8, 2, 1))]
- def make_table():
- import unicodedata as ud, re
- matcher= re.compile('^BOX DRAWINGS LIGHT (\S+)(?: AND (\S+))?$')
- table= array.array(b'u', ' ')*256
- for char_index in xrange(0x2500, 0x257f):
- char= unichr(char_index)
- match= matcher.match(ud.name(char))
- if match:
- mg1= match.group(1)
- mg2= match.group(2)
- table_index= value(mg1) | value(mg2)
- for other in OTHERS:
- table[table_index | other]= char
- return table
- def make_maze(rows, cols):
- table= make_table()
- last_row= [1] + [2]*cols
- for ix in xrange(rows):
- curr_row= [1] + [random.randrange(4) for _ in xrange(cols-2)] + [1]
- for idx in xrange(1, len(curr_row)):
- sys.stdout.write(table[
- 64*last_row[idx-1]
- + 16*last_row[idx]
- + 4*curr_row[idx-1]
- + curr_row[idx]])
- sys.stdout.write('\n')
- last_row= curr_row
- if __name__ == "__main__":
- make_maze(int(sys.argv[1]), int(sys.argv[2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement