advent_of_code/2023/11/11.py

73 lines
1.8 KiB
Python
Raw Normal View History

2024-03-20 05:26:03 +01:00
#!/usr/bin/env python
# 2023 - Advent Of Code 11
# file = 'input_example.txt'
file = 'input.txt'
# pylint: disable=consider-using-with
input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
print(f'map: ({len(input_lines[0])}, {len(input_lines)})')
# find empty lines
empty_lines = []
for y, _ in enumerate(input_lines):
if '#' not in input_lines[y]:
empty_lines.append(y)
print(f'empty lines: {empty_lines}')
# find empty columns
empty_columns = []
for x in range(0, len(input_lines[0])):
empty = True
for y, _ in enumerate(input_lines):
if input_lines[y][x] == '#':
empty = False
break
if empty:
empty_columns.append(x)
print(f'empty columns: {empty_columns}')
# expand lines
eline = "".join(['.'] * len(input_lines[0]))
offset = 0
for l in empty_lines:
input_lines.insert(l+offset, eline)
offset += 1
# expand columns
offset = 0
for c in empty_columns:
for y, line in enumerate(input_lines):
s = line[:c+offset] + '.' + line[c+offset:]
input_lines[y] = s
offset += 1
stars = []
for y, _ in enumerate(input_lines):
for x, _ in enumerate(_):
if input_lines[y][x] == '#':
stars.append((x, y))
print(f'stars: {len(stars)}, extended map: ({len(input_lines[0])}, {len(input_lines)})')
# for y in range(0, len(input_lines)):
# for x in range(0, len(input_lines[y])):
# print(input_lines[y][x], end='')
# print()
paths = []
for s1 in stars:
for s2 in stars:
if s1 != s2 and {s1, s2} not in paths:
paths.append({s1, s2})
print(f'number of paths: {len(paths)}')
# dist = math.abs(x2-x1) + math.abs(y2-y1)
path_sum = 0
for path in paths:
p = list(path)
dist = abs(p[0][0] - p[1][0]) + abs(p[0][1] - p[1][1])
path_sum += dist
# print(f'{path} dist: {dist}')
print(f'{path_sum=}')