Created
December 4, 2022 11:52
-
-
Save rtt/a7b27a9a8e845f7286b58367b9d22cc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def part1(inp): | |
overlaps = 0 | |
for pair in inp: | |
bounds = [] | |
for elf in pair.split(','): | |
lower, upper = elf.split('-') | |
bounds.append(set(range(int(lower), int(upper) + 1))) | |
if bounds[0] >= bounds[1] or bounds[1] >= bounds[0]: | |
overlaps = overlaps + 1 | |
return overlaps | |
def part2(inp): | |
contains = 0 | |
for pair in inp: | |
bounds = [] | |
for elf in pair.split(','): | |
lower, upper = elf.split('-') | |
bounds.append(set(range(int(lower), int(upper) + 1))) | |
if bounds[0] & bounds[1]: | |
contains = contains + 1 | |
return contains | |
if __name__ == '__main__': | |
with open('./input.txt') as f: | |
inp = f.read().strip().split('\n') | |
print(part1(inp)) | |
print(part2(inp)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment