Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Input: "0:", grid of .#, blank line, "1:", etc.,
- # then lines like "<width>x<height>: <space-delimited list of #s>"
- # (list = # copies of shape 0, 1, ...)
- # Output: how many regions can fit all the shapes in their lists?
- from polyomino.constant import MONOMINO
- from polyomino.board import Rectangle
- from polyomino.tileset import many
- from polyomino.error import CoverWithWrongSize
- shape = []
- tiles = []
- found_regions = False
- regions = []
- file = open("12_input.txt", "r")
- for line in file:
- line = line.replace("\n", "")
- if ": " in line:
- found_regions = True
- if found_regions:
- chunks = line.split(": ")
- dimensions, shape_counts_str = chunks[0], chunks[1]
- chunks = dimensions.split("x")
- width, height = int(chunks[0]), int(chunks[1])
- shape_counts = []
- for shape_count_str in shape_counts_str.split(" "):
- shape_counts.append(int(shape_count_str))
- regions.append([width, height, shape_counts])
- continue
- if ":" in line:
- continue
- if line == "":
- tile = []
- for x in range(0, len(shape)):
- for y in range(0, len(shape[0])):
- if shape[x][y] == "#":
- tile.append((x, y))
- tiles.append(tile)
- shape = []
- continue
- shape_row = []
- for character in line:
- shape_row.append(character)
- shape.append(shape_row)
- total = 0
- for region in regions:
- width, height, shape_counts = region[0], region[1], region[2]
- board = Rectangle(width, height)
- tile_set = many(MONOMINO)
- for index in range(0, len(tiles)):
- tile_set = tile_set.and_repeated_exactly(shape_counts[index], tiles[index])
- try:
- print ("Attempting to solve", region)
- problem = board.tile_with_set(tile_set).with_heuristics()
- # <this works for sample, but on real input it churns and dies>
- # if problem.solve() != None:
- # print (" Found a solution")
- # total += 1
- # else:
- # print (" Did not find a solution")
- # just calculate the upper bound and see if it works
- print (" May be a solution")
- total += 1
- except CoverWithWrongSize:
- print (" Did not find a solution (CoverWithWrongSize)")
- print (total)
Advertisement
Add Comment
Please, Sign In to add comment