-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay20-2.py
More file actions
65 lines (54 loc) · 1.86 KB
/
Copy pathDay20-2.py
File metadata and controls
65 lines (54 loc) · 1.86 KB
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
import os.path
import numpy as np
def search_monsters(image, sea_monster):
for i in range(len(image) - len(sea_monster)):
for j in range(len(image[i]) - len(sea_monster[0])):
monsterFound = True
monsterElements = []
for y in range(len(sea_monster)):
for x in range(len(sea_monster[y])):
if sea_monster[y][x] == ' ':
continue
elif sea_monster[y][x] == '#':
if image[i + y][j + x] != '#':
monsterFound = False
break
else:
monsterElements.append((i + y, j + x))
if not monsterFound:
break
if monsterFound:
for element in monsterElements:
image[element[0]][element[1]] = '@'
return image
def count_rough_water(image):
rough_waters = 0
for line in image:
for c in line:
if c == '#':
rough_waters += 1
return rough_waters
if not os.path.exists('../inputs/day20-2.txt'):
exec(open("./Day20.py").read())
with open('../inputs/day20-2.txt') as f:
image = []
for line in f.readlines():
image.append(list(line.strip()))
sea_monster = """ #
# ## ## ###
# # # # # # """
sea_monster = sea_monster.split('\n')
imageOptions = [
image,
np.rot90(image, 1).tolist(),
np.rot90(image, 2).tolist(),
np.rot90(image, 3).tolist(),
np.flipud(image).tolist(),
np.rot90(np.flipud(image), 1).tolist(),
np.rot90(np.flipud(image), 2).tolist(),
np.rot90(np.flipud(image), 3).tolist(),
]
rough_waters = []
for im in imageOptions:
rough_waters.append(count_rough_water(search_monsters(im, sea_monster)))
print(f"20-2: {min(rough_waters)}")