aoc/2022/day04.py

53 lines
1.2 KiB
Python

#!/usr/bin/env python3
from typing import TextIO
def part1(sections: TextIO) -> int:
overlaps: int = 0
for line in sections:
line = line.strip().split(",")
first_st = int(line[0].split("-")[0])
first_nd = int(line[0].split("-")[1])
sec_st = int(line[1].split("-")[0])
sec_nd = int(line[1].split("-")[1])
first = list(range(first_st, first_nd + 1))
second = list(range(sec_st, sec_nd + 1))
if first_st in second and first_nd in second:
overlaps += 1
elif sec_st in first and sec_nd in first:
overlaps += 1
return overlaps
def part2(sections: TextIO) -> int:
all_overlaps: int = 0
for line in sections:
line = line.strip().split(",")
first_st = int(line[0].split("-")[0])
first_nd = int(line[0].split("-")[1])
sec_st = int(line[1].split("-")[0])
sec_nd = int(line[1].split("-")[1])
first = list(range(first_st, first_nd + 1))
second = list(range(sec_st, sec_nd + 1))
if any(i in first for i in second):
all_overlaps += 1
return all_overlaps
if __name__ == "__main__":
print(part1(open("day04.txt")))
print(part2(open("day04.txt")))