Solve 2022 day 4 pt 1-2
This commit is contained in:
parent
62d19f09f6
commit
4c538f7683
1 changed files with 31 additions and 0 deletions
31
2022-python/output/day_04.py
Normal file
31
2022-python/output/day_04.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
p1 = sum(_p1(line) for line in data.split())
|
||||||
|
p2 = sum(_p2(line) for line in data.split())
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def _p1(data):
|
||||||
|
a1, a2, b1, b2 = ints(data)
|
||||||
|
return (a1 >= b1 and a2 <= b2) or (b1 >= a1 and b2 <= a2)
|
||||||
|
|
||||||
|
|
||||||
|
def _p2(data):
|
||||||
|
a1, a2, b1, b2 = ints(data)
|
||||||
|
|
||||||
|
return len(set(range(a1, a2 + 1)) & set(range(b1, b2 + 1))) > 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/04.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
|
|
||||||
|
assert p1 == 507
|
||||||
|
assert p2 == 897
|
||||||
Loading…
Add table
Reference in a new issue