advent-of-code/2022/python/day04.py

39 lines
796 B
Python
Raw Permalink Normal View History

2022-12-09 16:43:00 +00:00
import shared
def part1(x):
internal = 0
for pair in x:
2022-12-12 07:41:14 +00:00
l, r = pair.split(",")
l1, l2 = map(int, l.split("-"))
r1, r2 = map(int, r.split("-"))
2022-12-09 16:43:00 +00:00
if l1 <= r1 and l2 >= r2:
internal += 1
elif r1 <= l1 and r2 >= l2:
internal += 1
print(internal)
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
def part2(x):
internal = 0
for pair in x:
2022-12-12 07:41:14 +00:00
l, r = pair.split(",")
2022-12-09 16:43:00 +00:00
ll = list(sorted(map(int, l.split("-"))))
rl = list(sorted(map(int, r.split("-"))))
2022-12-12 07:41:14 +00:00
set1 = set(range(ll[0], ll[1] + 1))
set2 = set(range(rl[0], rl[1] + 1))
2022-12-09 16:43:00 +00:00
if bool(set1 & set2):
2022-12-12 07:41:14 +00:00
internal += 1
2022-12-09 16:43:00 +00:00
print(internal)
def main():
rows = [row for row in shared.load(4)]
part1(rows)
part2(rows)
if __name__ == "__main__":
main()