Guest User

Untitled

a guest
Dec 12th, 2025
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. # Input: "0:", grid of .#, blank line, "1:", etc.,
  2. #        then lines like "<width>x<height>: <space-delimited list of #s>"
  3. #        (list = # copies of shape 0, 1, ...)
  4. # Output: how many regions can fit all the shapes in their lists?
  5.  
  6. from polyomino.constant import MONOMINO
  7. from polyomino.board import Rectangle
  8. from polyomino.tileset import many
  9. from polyomino.error import CoverWithWrongSize
  10.  
  11. shape = []
  12. tiles = []
  13.  
  14. found_regions = False
  15. regions = []
  16.  
  17. file = open("12_input.txt", "r")
  18. for line in file:
  19.   line = line.replace("\n", "")
  20.  
  21.   if ": " in line:
  22.     found_regions = True
  23.  
  24.   if found_regions:
  25.     chunks = line.split(": ")
  26.     dimensions, shape_counts_str = chunks[0], chunks[1]
  27.     chunks = dimensions.split("x")
  28.     width, height = int(chunks[0]), int(chunks[1])
  29.     shape_counts = []
  30.     for shape_count_str in shape_counts_str.split(" "):
  31.       shape_counts.append(int(shape_count_str))
  32.     regions.append([width, height, shape_counts])
  33.     continue
  34.  
  35.   if ":" in line:
  36.     continue
  37.  
  38.   if line == "":
  39.     tile = []
  40.     for x in range(0, len(shape)):
  41.       for y in range(0, len(shape[0])):
  42.         if shape[x][y] == "#":
  43.           tile.append((x, y))
  44.     tiles.append(tile)
  45.     shape = []
  46.     continue
  47.  
  48.   shape_row = []
  49.   for character in line:
  50.     shape_row.append(character)
  51.   shape.append(shape_row)
  52.  
  53. total = 0
  54.  
  55. for region in regions:
  56.   width, height, shape_counts = region[0], region[1], region[2]
  57.   board = Rectangle(width, height)
  58.   tile_set = many(MONOMINO)
  59.   for index in range(0, len(tiles)):
  60.     tile_set = tile_set.and_repeated_exactly(shape_counts[index], tiles[index])
  61.   try:
  62.     print ("Attempting to solve", region)
  63.     problem = board.tile_with_set(tile_set).with_heuristics()
  64.  
  65. #   <this works for sample, but on real input it churns and dies>
  66. #   if problem.solve() != None:
  67. #     print ("  Found a solution")
  68. #     total += 1
  69. #   else:
  70. #     print ("  Did not find a solution")
  71.  
  72. #   just calculate the upper bound and see if it works
  73.     print ("  May be a solution")
  74.     total += 1
  75.  
  76.   except CoverWithWrongSize:
  77.     print ("  Did not find a solution (CoverWithWrongSize)")
  78.  
  79. print (total)
Advertisement
Add Comment
Please, Sign In to add comment