2022/day04: Complete!
ci/woodpecker/push/woodpecker Pipeline was successful Details

day07
earnest ma 2022-12-04 21:09:21 -05:00
parent d797b183f4
commit 3d2ce132b7
Signed by: earnest ma
GPG Key ID: A343F43342EB6E2A
4 changed files with 1067 additions and 1 deletions

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python3
from typing import TextIO
import pytest
calories = []

52
2022/day04.py Normal file
View File

@ -0,0 +1,52 @@
#!/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")))

1000
2022/day04.txt Normal file

File diff suppressed because it is too large Load Diff

15
2022/day04_test.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
from day04 import *
def test_part1() -> None:
sections = open("day04.txt")
assert part1(sections) == 588
sections.close()
def test_part2() -> None:
sections = open("day04.txt")
assert part2(sections) == 911
sections.close()