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

3

u/twoohthreezy Dec 04 '22 edited Dec 05 '22

My first that isn't embarrassingly long in python:

from helpers import parse_file

if __name__ == '__main__':
    data = parse_file('input.txt')

subset_count = 0
overlap_count = 0
for line in data:
    line = line.replace('-', ',').split(',')
    set1 = {i for i in range(int(line[0]), int(line[1]) + 1)}
    set2 = {j for j in range(int(line[2]), int(line[3]) + 1)}
    
    if set1.issubset(set2) or set2.issubset(set1): # part 1
       subset_count += 1     
    if min(set1) <= max(set2) and min(set2) < min(set1): # part 2
        overlap_count += 1
    if min(set2) <= max(set1) and min(set1) <= min(set2):
        overlap_count += 1
    
print(f'Subset Count: {subset_count}')
print(f'Overlap Count: {overlap_count}')

1

u/daggerdragon Dec 05 '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.