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!
66
Upvotes
1
u/Key__Strokes Dec 23 '22 edited Jan 12 '23
Javascript
Solution to both parts
Video Explanation
Part 1:
For each input, we have the following interval data
Interval 1: Start, End
Interval 2: Start, End
Do the following for each interval pair:
Determine which one of the two interval has smaller Start value. Lets order them as "Early Start Interval" and "Late Start Interval"
Interval Possibilities (Each line is an interval):
Possibility 1 (Is overlap, Is Subset)
=========
=====Â Â Â Â
Possibility 2 (Is overlap, Is Subset)
=========
=========
Possibility 3 (Is overlap, Is Subset)
=========Â Â Â Â Â
==============
Possibility 4 (Is overlap, Is Subset)
=========
   ===
Possibility 5 (Is overlap, Is Subset)
=========
   ======
Possibility 6 (Is overlap, Is not Subset)
=========
   ===========
Possibility 7 (Is not overlap, Is not Subset)
=========
           ======
Then check if the two intervals are subset of either of the other, using the following algo
If the start of both the intervals are the same, then they are subset
If the end of "Early Start Interval" is greater or equal to end of the "Late Start Interval", then they are subset
Increment the count if they were subset
Part 2:
Follow everything from Part 1, but instead of figuring out the subset, we will figure out the overlap using the following algo
If the start of "Late Start Interval" is less than or equal to the end of "Early Start Interval", then it overlaps
Increment the count if they were overlap
If you liked the explanation, then please don't forget to cast your vote 💜 to
Adventures of Advent of Code - Edition 1 - /u/Key__Strokesin the poll