(update) 2022 day 4
This commit is contained in:
6
2022/04/example
Normal file
6
2022/04/example
Normal file
@ -0,0 +1,6 @@
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
||||
1000
2022/04/input
Normal file
1000
2022/04/input
Normal file
File diff suppressed because it is too large
Load Diff
32
2022/04/main.py
Normal file
32
2022/04/main.py
Normal file
@ -0,0 +1,32 @@
|
||||
def part1(gh: list):
|
||||
|
||||
l, r = [x.split("-") for x in gh]
|
||||
|
||||
v = 0
|
||||
v += (
|
||||
1
|
||||
if (int(l[0]) >= int(r[0]) and int(l[1]) <= int(r[1]))
|
||||
or (int(r[0]) >= int(l[0]) and int(r[1]) <= int(l[1]))
|
||||
else 0
|
||||
)
|
||||
return v
|
||||
|
||||
|
||||
def part2(gh: list):
|
||||
l, r = [x.split("-") for x in gh]
|
||||
first = [x for x in range(int(l[0]), int(l[1]) + 1)]
|
||||
second = [x for x in range(int(r[0]), int(r[1]) + 1)]
|
||||
|
||||
overlap = [x for x in first if x in second]
|
||||
return 1 if overlap.__len__() > 0 else 0
|
||||
|
||||
|
||||
with open("input", "+r") as file:
|
||||
p1 = 0
|
||||
p2 = 0
|
||||
for f in file.readlines():
|
||||
d = f.strip().split(",")
|
||||
p1 += part1(d)
|
||||
p2 += part2(d)
|
||||
print("part 1:", p1)
|
||||
print("part 2:", p2)
|
||||
Reference in New Issue
Block a user