/
aoc_20a.py
71 lines (60 loc) · 1.8 KB
/
aoc_20a.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
fin = open('input_20.txt')
lines = fin.readlines()
fin.close()
tiles={} # ndarrays with the tile data
borders = {} # two lists with four numbers each for each tile
tile_with_border = {} # for each border-id a list of tiles with this id
def getnumber(row):
bits = '0b'
for i in row:
bits += str(i)
return int(bits,base=2)
def getborders(arr):
thisarr = arr.copy()
list1 = []
list2 = []
for i in range(4):
toprow = thisarr[0]
worpot = toprow[::-1]
list1.append(getnumber(toprow))
list2.append(getnumber(worpot))
thisarr = np.rot90(thisarr)
return list1,list2
for lc,line in enumerate(lines):
if line.startswith('Tile'):
thistile = np.zeros((10,10),dtype='int8')
tile_id=int(line[5:9])
for i in range(1,11):
tline = lines[lc+i]
for c,char in enumerate(tline):
if char == '#':
thistile[i-1,c]=1
tiles[tile_id]=thistile
# print(thistile)
borders[tile_id]=getborders(thistile)
# print(borders[tile_id])
for orientation in borders[tile_id]:
for element in orientation:
try:
tile_with_border[element].append(tile_id)
except KeyError:
tile_with_border[element] = [tile_id]
edgeborders = set()
for border in tile_with_border:
tiles = tile_with_border[border]
if len(tiles) ==1:
edgeborders.add(border)
# print(edgeborders)
edgeproduct = 1
for tile in borders:
b = borders[tile]
cornercount = 0
for orientation in b:
for bid in orientation:
if bid in edgeborders:
cornercount +=1
if cornercount == 4:
print(tile)
edgeproduct *= tile
print(edgeproduct)