All checks were successful
continuous-integration/drone/push Build is passing
65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
from pathlib import Path
|
|
|
|
|
|
def enhance(algorithm, in_image, boundary):
|
|
out_image = []
|
|
height = len(in_image)
|
|
width = len(in_image[0])
|
|
for i in range(-boundary, height + boundary):
|
|
line = ''
|
|
valid = False
|
|
for j in range(-boundary, width + boundary):
|
|
idx = ''
|
|
for k in range(i - 1, i + 2):
|
|
for l in range(j - 1, j + 2):
|
|
if 0 <= k < height and \
|
|
0 <= l < width and \
|
|
'#' == in_image[k][l]:
|
|
idx += '1'
|
|
else:
|
|
idx += '0'
|
|
idx = int(idx, 2)
|
|
if not valid and '#' == algorithm[idx]:
|
|
valid = True
|
|
line += algorithm[idx]
|
|
if valid:
|
|
out_image.append(line)
|
|
return out_image
|
|
|
|
|
|
def part_1(input):
|
|
result = 0
|
|
algorithm = input[0].rstrip()
|
|
image = []
|
|
for line in input[2:]:
|
|
image.append(line.rstrip())
|
|
image = enhance(algorithm, image, 3)
|
|
image = enhance(algorithm, image, -1)
|
|
for line in image:
|
|
result += line.count('#')
|
|
|
|
print("Part 1 result:", result)
|
|
|
|
|
|
def part_2(input):
|
|
result = 0
|
|
algorithm = input[0].rstrip()
|
|
image = []
|
|
for line in input[2:]:
|
|
image.append(line.rstrip())
|
|
for _ in range(25):
|
|
image = enhance(algorithm, image, 3)
|
|
image = enhance(algorithm, image, -1)
|
|
for line in image:
|
|
result += line.count('#')
|
|
print("Part 2 result:", result)
|
|
|
|
|
|
input = list()
|
|
p = Path(__file__).with_name('input.txt')
|
|
with open(p) as f:
|
|
input = f.readlines()
|
|
part_1(input)
|
|
part_2(input)
|