You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.4 KiB
Python

#!/usr/bin/env python3
"""
Part 1
"""
import sys
import re
ENGINE_MAP = []
for line in sys.stdin.readlines():
line = line.strip()
ENGINE_MAP.append(line)
# print(line)
print()
# print(ENGINE_MAP)
number_matches = set()
for x, line in enumerate(ENGINE_MAP):
for y, char in enumerate(line):
if re.match(r"[^0-9\.]", char):
# print(f"{x},{y}: {char}")
for d_x in [-1, 0, 1]:
for d_y in [-1, 0, 1]:
coord_x = x + d_x
coord_y = y + d_y
if coord_x < 0 or coord_x > len(ENGINE_MAP) - 1:
continue
if coord_y < 0 or coord_y > len(ENGINE_MAP[coord_x]) - 1:
continue
if re.match(r"[0-9]", ENGINE_MAP[coord_x][coord_y]):
while coord_y > 0 and re.match(
r"[0-9]", ENGINE_MAP[coord_x][coord_y - 1]
):
coord_y -= 1
number_matches.add((coord_x, coord_y))
# print()
# print(number_matches)
numbers = []
RESULT = 0
print()
for match in number_matches:
x, y = match
line = ENGINE_MAP[x]
number = line[y]
while y < len(line) - 1 and re.match(r"[0-9]", line[y + 1]):
y += 1
number += line[y]
number = int(number)
# print(f"{x},{y}: {number}")
RESULT += number
print()
print(RESULT)