This commit is contained in:
71
day-13/day-13.py
Normal file
71
day-13/day-13.py
Normal file
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def fold(i, dots):
|
||||
new_dots = []
|
||||
if i[0] == 'x':
|
||||
for d in dots:
|
||||
n = (i[1] - abs(d[0] - i[1]), d[1])
|
||||
if d[0] != i[1] and n not in new_dots:
|
||||
new_dots.append(n)
|
||||
elif i[0] == 'y':
|
||||
for d in dots:
|
||||
n = (d[0], i[1] - abs(d[1] - i[1]))
|
||||
if d[1] != i[1] and n not in new_dots:
|
||||
new_dots.append(n)
|
||||
|
||||
return new_dots
|
||||
|
||||
|
||||
def part_1(input):
|
||||
result = 0
|
||||
dots = []
|
||||
instructions = []
|
||||
for line in input:
|
||||
if ',' in line:
|
||||
x, y = line.strip().split(',')
|
||||
dots.append((int(x), int(y)))
|
||||
elif 'fold along' in line:
|
||||
d, c = line.strip().split()[-1].split('=')
|
||||
instructions.append([d, int(c)])
|
||||
dots = fold(instructions.pop(0), dots)
|
||||
result = len(dots)
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
result = 0
|
||||
dots = []
|
||||
instructions = []
|
||||
for line in input:
|
||||
if ',' in line:
|
||||
x, y = line.strip().split(',')
|
||||
dots.append((int(x), int(y)))
|
||||
elif 'fold along' in line:
|
||||
d, c = line.strip().split()[-1].split('=')
|
||||
instructions.append([d, int(c)])
|
||||
|
||||
while len(instructions):
|
||||
dots = fold(instructions.pop(0), dots)
|
||||
max_x = 0
|
||||
max_y = 0
|
||||
for d in dots:
|
||||
max_x = max(max_x, d[0] + 1)
|
||||
max_y = max(max_y, d[1] + 1)
|
||||
for y in range(max_y):
|
||||
for x in range(max_x):
|
||||
if (x, y) in dots:
|
||||
print('#', end='')
|
||||
else:
|
||||
print('.', end='')
|
||||
print()
|
||||
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)
|
||||
Reference in New Issue
Block a user