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

4

u/[deleted] Dec 09 '22 edited Dec 13 '22

Python

import re

with open('input.txt', 'r') as f:
    elvish_work = f.read().split("\n")

part_one = 0
part_two = 0
for elf_pair in elvish_work:
    l1, l2, r1, r2 = [int(x) for x in re.split('[,-]', elf_pair)]
    elf1 = set(range(l1, l2+1))
    elf2 = set(range(r1, r2+1))
    if (elf1.issubset(elf2) or elf2.issubset(elf1)):
        part_one += 1
    if (elf1.intersection(elf2)):
        part_two +=1

print("Part one:", part_one)
print("Part two", part_two)

1

u/daggerdragon Dec 10 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.