Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- with open("input") as file:
- data = file.read().splitlines()
- num = []
- sym = []
- part1 = 0
- for line in data:
- num.append([(int(m.group()),m.start(), m.end() - 1) for m in re.finditer(r'(\d+)', line)])
- sym.append([m.start() for m in re.finditer(r'[^\d.]', line)])
- for i, x in enumerate(num):
- for n, s, e in x:
- if ((s - 1 in sym[i]) or (e + 1 in sym[i]) or
- (i != len(num) - 1 and any(x in sym[i + 1] for x in range(s - 1, e + 2))) or
- (i != 0 and any(x in sym[i - 1] for x in range(s - 1, e + 2)))):
- part1 += n
- print(part1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement