Skip to content

Instantly share code, notes, and snippets.

@rtt
Created December 4, 2022 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtt/a7b27a9a8e845f7286b58367b9d22cc7 to your computer and use it in GitHub Desktop.
Save rtt/a7b27a9a8e845f7286b58367b9d22cc7 to your computer and use it in GitHub Desktop.
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