Advertisement
hhoppe

Advent of code 2023 day 1

Dec 1st, 2023
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. def day1(s, *, part2=False):
  2.   values = {str(i): i for i in range(10)}
  3.   if part2:
  4.     strings = 'zero one two three four five six seven eight nine'.split()
  5.     values |= {name: index for index, name in enumerate(strings)}
  6.   pattern = f"({'|'.join(values)})"
  7.   total = 0
  8.   for line in s.splitlines():
  9.     str0 = re.search(pattern, line).group(1)
  10.     str1 = re.search(f'.*{pattern}', line).group(1)
  11.     total += values[str0] * 10 + values[str1]
  12.   return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement