Refactor python solution for 2022 day 4 even more

This commit is contained in:
hedy 2022-12-04 18:40:18 +08:00
parent 77eb97d19b
commit 62eb085026
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
1 changed files with 7 additions and 2 deletions

View File

@ -5,9 +5,14 @@ from aocd import submit
with open("input.txt") as f:
lines = [ list(map(int, re.split(",|-", l))) for l in f.read().strip().split('\n') ]
n = len(list(filter(lambda l: (l[0] >= l[2] and l[1] <= l[3]) or (l[2] >= l[0] and l[3] <= l[1]), lines)))
# wtf?
n = len(list(filter(lambda l: (l[0]<=l[2]<=l[3]<=l[1] or l[2]<=l[0]<=l[1]<=l[3]), lines)))
print(n)
submit(n, year=2022, part="a", day=4)
n = len(list(filter(lambda l: (len(set(range(l[0], l[1]+1)) & set(range(l[2], l[3]+1))) > 0), lines)))
n = len(list(filter(lambda l: (l[0]<=l[3]>=l[2]<=l[1]), lines)))
print(n)
submit(n, year=2022, part="b", day=4)
# old implementation
# n = len(list(filter(lambda l: (l[0] >= l[2] and l[1] <= l[3]) or (l[2] >= l[0] and l[3] <= l[1]), lines)))
# n = len(list(filter(lambda l: (len(set(range(l[0], l[1]+1)) & set(range(l[2], l[3]+1))) > 0), lines)))