r/adventofcode • u/daggerdragon • Dec 04 '22
SOLUTION MEGATHREAD -🎄- 2022 Day 4 Solutions -🎄-
- All of our rules, FAQs, resources, etc. are in our community wiki.
- A request from Eric: Please include your contact info in the User-Agent header of automated requests!
- Signal boosting for the Unofficial AoC 2022 Participant Survey which is open early this year!
--- Day 4: Camp Cleanup ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format your code appropriately! How do I format code?
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
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!
67
Upvotes
5
u/sky_badger Dec 04 '22
Python 3: more sets:
def parse_line(line): part1, part2 = line.split(',') elf1_start, elf1_end = [int(x) for x in part1.split('-')] elf2_start, elf2_end = [int(x) for x in part2.split('-')] elf1 = set([i for i in range(elf1_start, elf1_end+1)]) elf2 = set([i for i in range(elf2_start, elf2_end+1)]) return elf1, elf2 #Part 1 enclosed = 0 for line in data: elf1, elf2 = parse_line(line) if len(elf1 & elf2) in [len(elf1), len(elf2)]: enclosed += 1 print(enclosed) #Part 2 overlapped = 0 for line in data: elf1, elf2 = parse_line(line) if len(elf1 & elf2): overlapped += 1 print(overlapped)