advent-of-code/2022/day4.py
Mikaël Capelle f76767ca6d Add day 4.
2022-12-05 19:09:55 +01:00

20 lines
506 B
Python

# -*- encoding: utf-8 -*-
import sys
lines = [line.strip() for line in sys.stdin.readlines()]
def make_range(value: str) -> set[int]:
parts = value.split("-")
return set(range(int(parts[0]), int(parts[1]) + 1))
sections = [tuple(make_range(part) for part in line.split(",")) for line in lines]
score_1 = sum(s1.intersection(s2) in (s1, s2) for s1, s2 in sections)
print(f"score 1 is {score_1}")
score_2 = sum(bool(s1.intersection(s2)) for s1, s2 in sections)
print(f"score 1 is {score_2}")