Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2022 Day 1
- Advent of Code 2022 Day 2
- Advent of Code 2022 Day 3
- Advent of Code 2022 Day 4
- Advent of Code 2022 Day 5
- Advent of Code 2022 Day 6
- Advent of Code 2022 Day 7
- Advent of Code 2022 Day 8
- Advent of Code 2022 Day 9
- Advent of Code 2022 Day 10
- Advent of Code 2022 Day 11
- Advent of Code 2022 Day 12
- Advent of Code 2022 Day 13
- Advent of Code 2022 Day 14
- Advent of Code 2022 Day 15
- Advent of Code 2022 Day 16
- Advent of Code 2022 Day 17
- Advent of Code 2022 Day 18
- Advent of Code 2022 Day 19
- Advent of Code 2022 Day 20
- Advent of Code 2022 Day 21
- Advent of Code 2022 Day 22
- Advent of Code 2022 Day 23
- Advent of Code 2022 Day 24
- Advent of Code 2022 Day 25
-
Older post
-
Newer post
Advent of Code 2022 Day 4
Day 4: Camp Cleanup
https://adventofcode.com/2022/day/4
Today, the elves have to clean up their camp.
- Each area of the camp has a unique ID number.
- Each elf is assigned a range of those IDs to clean.
The elves are split into groups of 2.
Your input is a big list of the section assignments for each pair. Each line of the input represents a pair of elves.
The two elves in a line are seperated by a comma ,
.
The start and end (inclusive) of their assigned range is seperated by a dash -
.
An example input looks like this:
Part 1
The question asks to find in how many pairs one range fully contains the other.
I looped through every line of the input, only keps the ones with a full overlap, and counted how many were left.
I started off with this pseudocode that I then filled in:
A range completely overlaps an other range if:
Its minimum is lower (or equal) than the other minimum and its maximum is higher (or equal) than the other maximum.
I did this check twice, once for each elf.
- Once to check if elf1 completely overlaps with elf2
- Once to check if elf2 completely overlaps with elf1.
The longest part of this solution is parsing those strings into 4 numbers: min1
, max1
, min2
, and max2
.
Part 2
The question asks to find in how many pairs one range overlaps the other (partially, or completely).
The overall setup and the number parsing piece are identical to part1. The only change is the line that determines if a line is kept or not.
A range overlaps an other when:
the biggest minimum is smaller (or equal) than the smallest maximum.