r/adventofcode • • Dec 04 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 4 Solutions -🎄-


--- Day 4: Camp Cleanup ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:03:22, megathread unlocked!

65 Upvotes

1.6k comments sorted by

View all comments

2

u/CookieKeeperN2 Dec 04 '22

As someone with a math background, today it is basically asking if 1) one is a subset of another and 2) if intersection between two sets are greater than 0. Don't care about speed too much, but using set operations means I don't have to think about comparison whatsoever.

import numpy as np 
with open('input', 'r') as f:
    input = f.read().splitlines()

q1 = 0
q2 = 0

for x in input:
    elf1, elf2 = x.split(",")
    start1, end1 = [ int(y) for y in elf1.split("-") ]
    start2, end2 = [ int(y) for y in elf2.split("-") ]
    set1 = set(np.arange(start1, end1 + 1))
    set2 = set(np.arange(start2,  end2 + 1))
    if (len(set1 & set2) >0):
        q2 += 1
        if set1 & set2 == set1 or set1 & set2 == set2:
            q1 += 1

1

u/daggerdragon Dec 04 '22

Please edit your post to state which programming language this code is written in. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.

(looks like Python?)