-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay20.py
More file actions
112 lines (88 loc) · 3.09 KB
/
Day20.py
File metadata and controls
112 lines (88 loc) · 3.09 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import Utility
filePath = 'input/day20/part1.txt'
def getTransformLookup() -> dict[int, bool]:
inputLines = Utility.getLinesFromFile(filePath)
line = inputLines[0]
result = {}
for i in range(len(line)):
result.update({i: line[i] == '#'})
return result
class Image:
def __init__(self) -> None:
inputLines = Utility.getLinesFromFile(filePath)
imageLines = inputLines[2:]
self.imageLookup = {}
for i, l in enumerate(imageLines):
for j, c in enumerate(l):
self.imageLookup.update({(j, i): c == '#'})
self.minY = 0
self.minX = 0
self.maxY = len(imageLines)
self.maxX = len(imageLines[0])
self.currentBackground = False
self.transformLookup = getTransformLookup()
def getPixel(self, x: int, y: int) -> bool:
if (x < self.minX or x >= self.maxX or y < self.minY or y >= self.maxY):
return self.currentBackground
return self.imageLookup[(x, y)]
def iterate(self):
newImageLookup = {}
for x in range(self.minX - 1, self.maxX + 1):
for y in range(self.minY - 1, self.maxY + 1):
newImageLookup.update({(x, y): self.determineNewPixel(x, y)})
self.imageLookup = newImageLookup
self.minX -= 1
self.maxX += 1
self.minY -= 1
self.maxY += 1
if self.currentBackground:
self.currentBackground = self.transformLookup[511]
else:
self.currentBackground = self.transformLookup[0]
def determineNewPixel(self, x, y) -> bool:
bitBuildup = []
for yDelta in range(-1, 2):
for xDelta in range(-1, 2):
bitBuildup.append('1' if self.getPixel(
x + xDelta, y + yDelta) else '0')
bitString = ''.join(bitBuildup)
value = int(bitString, 2)
return self.transformLookup[value]
def __repr__(self) -> str:
outputBuildup = []
for y in range(self.minY - 1, self.maxY + 1):
lineBuildup = []
for x in range(self.minX - 1, self.maxX + 1):
lineBuildup.append('#' if self.getPixel(x, y) else '.')
outputBuildup.append(''.join(lineBuildup))
outputBuildup.append('\n')
return ''.join(outputBuildup)
def getLitCount(self) -> int:
if(self.currentBackground):
raise Exception('infinite lit pixels')
count = 0
for x in range(self.minX, self.maxX):
for y in range(self.minY, self.maxY):
if self.imageLookup[(x, y)]:
count += 1
return count
def iterateNTimes(self, n):
for i in range(n):
self.iterate()
def solvePart1():
image = Image()
print(image)
image.iterate()
print(image)
image.iterate()
print(image)
print('Solution to part1:')
print(image.getLitCount())
def solvePart2():
image = Image()
image.iterateNTimes(50)
print('Solution to part2:')
print(image.getLitCount())
if(__name__ == '__main__'):
solvePart1()
solvePart2()