advent-of-code/2022/day4.py

20 lines
509 B
Python
Raw Normal View History

2022-12-05 18:09:55 +00:00
# -*- 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]
2022-12-06 14:28:46 +00:00
score_1 = sum(s1.issubset(s2) or s2.issubset(s1) for s1, s2 in sections)
2022-12-05 18:09:55 +00:00
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}")