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.

i would have finished both parts in a couple minutes but ranges excluding the end fucked me up for so goddamn long, i just couldn't figure out what was wrong :marseyraging:

with open("day4input.txt") as f:
    pairs = [line for line in f]

# part 1

full_overlaps = 0

for pair in pairs:
    pair = pair.split(",") # [1-2, 2-3] 
    pair = [elf.split("-") for elf in pair] # [(1,2), (2,3)]
    elf1 = set(range(int(pair[0][0]), int(pair[0][1])+1))
    elf2 = set(range(int(pair[1][0]), int(pair[1][1])+1)) 
    if elf1.issubset(elf2) or elf2.issubset(elf1):
        full_overlaps += 1

print(full_overlaps)

# part 2 

overlaps = 0

for pair in pairs:
    pair = pair.split(",") # [1-2, 2-3] 
    pair = [elf.split("-") for elf in pair] # [(1,2), (2,3)]
    elf1 = set(range(int(pair[0][0]), int(pair[0][1])+1))
    elf2 = set(range(int(pair[1][0]), int(pair[1][1])+1)) 
    if elf1.intersection(elf2) != set():
        overlaps += 1

print(overlaps)
Jump in the discussion.

No email address required.

i lost 10 minutes because python didn't parse out the \n :marseyrope:

Jump in the discussion.

No email address required.

hint: use splitlines() instead of split('\n')

Jump in the discussion.

No email address required.

:marseycheerup: it's python's fault, not ours

Jump in the discussion.

No email address required.

this got me for... way too long :marseyfedpostglow: as well :marseyclapping:

Jump in the discussion.

No email address required.

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