Advertisement
icarussiano

Day 3 part 1

Dec 3rd, 2023
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. import re
  2. with open("input") as file:
  3.     data = file.read().splitlines()
  4. num = []
  5. sym = []
  6. part1 = 0
  7.  
  8. for line in data:
  9.     num.append([(int(m.group()),m.start(), m.end() - 1) for m in re.finditer(r'(\d+)', line)])
  10.     sym.append([m.start() for m in re.finditer(r'[^\d.]', line)])
  11.  
  12. for i, x in enumerate(num):
  13.     for n, s, e in x:
  14.         if ((s - 1 in sym[i]) or (e + 1 in sym[i]) or
  15.                 (i != len(num) - 1 and any(x in sym[i + 1] for x in range(s - 1, e + 2))) or
  16.                 (i != 0 and any(x in sym[i - 1] for x in range(s - 1, e + 2)))):
  17.             part1 += n
  18.  
  19. print(part1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement