def read_input(file_name):
lines_list =open(file_name).read().splitlines()
tiles_dict = {}
for line in lines_list:
if "Tile" in line:
tile_id = line.split(" ")[1][:-1]
image = []
i = 0
elif not i==10:
image.append([char for char in line])
i += 1
else:
tiles_dict[tile_id] = image
tiles_dict[tile_id] = image
return tiles_dict
def rotate_matrix(m, rottype):
if rottype == "0":
return m
elif rottype == "1":
return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
else:
return rotate_matrix([[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)], str(int(rottype)-1))
def get_row(m,i):
return m[i]
def flip_matrix(m, fliptype):
if fliptype == "h":
return [[m[j][i] for i in range(len(m[0])-1,-1,-1)] for j in range(len(m))]
elif fliptype == "v":
return [[m[j][i] for i in range(len(m))] for j in range(len(m[0])-1,-1,-1)]
else:
return m
def strip_matrix(m):
return [[m[j][i] for i in range(1,len(m)-1)] for j in range(1,len(m)-1)]
def get_column(m, j):
return [m[i][j] for i in range(len(m))]
def matching_edges(m,n):
edge_col_rows = [0,len(m)-1]
matching_edges = 0
orientation = "F"
for m_edge, n_edge in zip(edge_col_rows, edge_col_rows[::-1]):
if get_row(m, m_edge) == get_row(n, n_edge):
if m_edge == 0:
return "N"
else:
return "S"
if get_column(m, m_edge) == get_column(n, n_edge):
if m_edge == 0:
return "W"
else:
return "E"
return orientation
def get_equivalent_combinations():
test_matrix = [['a','b','c'],
['d','e','f'],
['g','h','i']]
tile_values = test_matrix
neigh_values = test_matrix
equivalent_combinations = {}
for rotation in ["0","1","2","3"]:
for flipping in ["n", "h", "v"]:
for neigh_rotation in ["0","1","2","3"]:
for neigh_flipping in ["n", "h", "v"]:
rotated_matrix = rotate_matrix(tile_values, rotation)
neigh_rotated_matrix = rotate_matrix(neigh_values, neigh_rotation)
flipped_matrix = flip_matrix(rotated_matrix, flipping)
neigh_flipped_matrix = flip_matrix(neigh_rotated_matrix, neigh_flipping)
key_tuple = (rotation+flipping, neigh_rotation+neigh_flipping)
orientation_match = matching_edges(flipped_matrix, neigh_flipped_matrix)
if flipped_matrix == neigh_flipped_matrix and not key_tuple[0]==key_tuple[1]:
equivalent_combinations[key_tuple[0]] = key_tuple[1]
sorted_combinations = sorted(equivalent_combinations)
return sorted_combinations[len(sorted_combinations)//2:]
def find_east_candidates(key_west, current_rotation):
east_keys = {subkey:key for key, value in matching_info[key_west].items() for subkey, subvalue in value.items() if subvalue == "E"}
matching_keys_east = [[key,subkey] for key, subkey in east_keys.items() if key[0] == current_rotation]
return matching_keys_east
def find_south_candidates(key_west, current_rotation):
south_keys = {subkey:key for key, value in matching_info[key_west].items() for subkey, subvalue in value.items() if subvalue == "S"}
matching_keys_south = [[key,subkey] for key, subkey in south_keys.items() if key[0] == current_rotation]
return matching_keys_south
def check_and_return_image(final_image):
for i in range(1, len(final_image)):
east_cand = find_east_candidates(final_image[0][i-1][0], final_image[0][i-1][1])
if len(east_cand)>0:
final_image[0][i] = [east_cand[0][1], east_cand[0][0][1]]
else:
return False, final_image
for j in range (1, len(final_image)):
south_cand = find_south_candidates(final_image[j-1][0][0], final_image[j-1][0][1])
if len(south_cand)>0:
final_image[j][0] = [south_cand[0][1], south_cand[0][0][1]]
else:
return False, final_image
for i in range(1, len(final_image)):
east_cand = find_east_candidates(final_image[j][i-1][0], final_image[j][i-1][1])
if len(east_cand)>0:
final_image[j][i] = [east_cand[0][1], east_cand[0][0][1]]
else:
return False, final_image
return True, final_image
def get_image_from_corner(corner):
final_image = [["" for j in range(square_len)] for i in range(square_len)]
for rotation in ["0","1","2","3"]:
for flipping in ["n", "h", "v"]:
final_image[0][0] = [corner, "0n"]
final_image[0][0][1] = rotation+flipping
is_found, final_image = check_and_return_image(final_image)
if is_found:
return final_image
return []
def pretty_print_tile(m):
for row in m:
for char in row:
print(char, end="")
print("")
print("")
print("")
def check_if_monster(i,j, this_image):
for j_m, j_tile in enumerate(monster_list):
for i_m, monster_tile in enumerate(j_tile):
if i+i_m >= len(big_image):
return False
if j+j_m >= len(big_image):
return False
if monster_tile == "#":
if this_image[j+j_m][i+i_m] != "#":
return False
return True
def count_seats(new_matrix):
return sum([1 for line in new_matrix for char in line if char=="#"])