advent-of-code/2022/day3.py

24 lines
609 B
Python
Raw Normal View History

2022-12-03 10:42:09 +00:00
import string
import sys
lines = [line.strip() for line in sys.stdin.readlines()]
# extract content of each part
parts = [(set(line[: len(line) // 2]), set(line[len(line) // 2 :])) for line in lines]
# priorities
priorities = {c: i + 1 for i, c in enumerate(string.ascii_letters)}
# part 1
part1 = sum(priorities[c] for p1, p2 in parts for c in p1.intersection(p2))
2023-12-09 08:54:53 +00:00
print(f"answer 1 is {part1}")
2022-12-03 10:42:09 +00:00
# part 2
n_per_group = 3
part2 = sum(
priorities[c]
for i in range(0, len(lines), n_per_group)
2023-01-06 12:48:18 +00:00
for c in set(lines[i]).intersection(*lines[i + 1 : i + n_per_group])
2022-12-03 10:42:09 +00:00
)
2023-12-09 08:54:53 +00:00
print(f"answer 2 is {part2}")