64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
import numpy as np
|
|
|
|
with open(r'day12/input.txt', 'r') as input:
|
|
lines = input.read().split('\n')[:-1]
|
|
|
|
def array_in(arr,lst):
|
|
for oth in lst:
|
|
if np.array_equal(arr,oth): return True
|
|
return False
|
|
|
|
def is_compat(grid,tile,x,y):
|
|
return (not 2 in (grid[x-1:x+2,y-1:y+2]+tile))
|
|
|
|
def add(grid,tile,x,y):
|
|
grid[x-1:x+2,y-1:y+2]+=tile
|
|
|
|
def find_solution(grid,reqs,x0 = 1,y0 = 1):
|
|
dim = np.shape(grid)
|
|
if reqs == []:
|
|
return True
|
|
for tile in tiles[reqs[0]]:
|
|
for x in range(x0, dim[0]-1):
|
|
for y in range(y0 if x == x0 else 1, dim[1]-1):
|
|
if is_compat(grid,tile,x,y):
|
|
add(grid,tile,x,y)
|
|
if find_solution(grid, reqs[1:]): return True
|
|
add(grid,-tile,x,y)
|
|
return False
|
|
|
|
|
|
|
|
|
|
tiles = []
|
|
areas = []
|
|
|
|
i = -1
|
|
for line in lines:
|
|
if 'x' in line:
|
|
dim,reqs = line.split(':')
|
|
dim = list(map(int,dim.split('x')))
|
|
reqs = list(map(int,reqs.strip().split(' ')))
|
|
areas.append((dim,reqs))
|
|
elif ':' in line:
|
|
i+=1
|
|
tiles.append([])
|
|
elif line != '':
|
|
tiles[i].append(list(map(lambda x: 1 if x=='#' else 0,list(line))))
|
|
|
|
for i,tile in enumerate(tiles.copy()):
|
|
base = np.array(tile)
|
|
flips_n_rots = [base]
|
|
for rot in [base,np.rot90(base),np.rot90(np.rot90(base)),np.rot90(np.rot90(np.rot90(base)))]:
|
|
for flip in [rot,np.flipud(rot)]:
|
|
if not array_in(flip, flips_n_rots) : flips_n_rots.append(flip)
|
|
tiles[i] = flips_n_rots
|
|
|
|
for dim,reqs in areas:
|
|
area = np.zeros(dim)
|
|
reqs_list = []
|
|
for i in range(len(reqs)):
|
|
for _ in range(reqs[i]):
|
|
reqs_list.append(i)
|
|
print(dim,reqs_list)
|
|
print(find_solution(area,reqs_list))
|