Unable to load image

Advent of code day 4 :marseysad:

This is the advent of code day 4 thread for bragging about your solution and bullying people who haven't gotten it yet :marseyill:

35
Jump in the discussion.

No email address required.

Part 1:

with open('input.txt') as f:
    lines = f.readlines()
  
total_overlap = 0
any_overlap = 0
  
for line in lines:
    elf1, elf2 = line.strip().split(',')
    elf1_start, elf1_end = elf1.split('-')
    elf2_start, elf2_end = elf2.split('-')
    
    elf1_start = int(elf1_start)
    elf1_end = int(elf1_end)
    elf2_start = int(elf2_start)
    elf2_end = int(elf2_end)
    
    if elf1_start <= elf2_start and elf1_end >= elf2_end:
        total_overlap += 1
    elif elf1_start >= elf2_start and elf1_end <= elf2_end:
        total_overlap += 1
        
    if elf1_start <= elf2_end and elf1_start >= elf2_start:
        any_overlap += 1
    elif elf2_start <= elf1_end and elf2_end >= elf1_start:
        any_overlap += 1


print("There are " + str(total_overlap) + " total overlapping sets")
print("There are " + str(any_overlap) + " overlapping sets")
Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.