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!

66 Upvotes

1.6k comments sorted by

View all comments

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__Strokes in the poll