day 12
This commit is contained in:
parent
e086476976
commit
346b97df52
3 changed files with 1118 additions and 0 deletions
64
day12/1.py
Normal file
64
day12/1.py
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
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))
|
||||||
24
day12/1bad.py
Normal file
24
day12/1bad.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
with open(r'day12/input.txt', 'r') as input:
|
||||||
|
lines = input.read().split('\n')[:-1]
|
||||||
|
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))))
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
for dim,reqs in areas:
|
||||||
|
count += 1 if dim[0]*dim[1] >= sum(reqs)*9 else 0
|
||||||
|
print(count)
|
||||||
1030
day12/input.txt
Normal file
1030
day12/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue