AOC 22 Day 4 🧼
Advent of Code 2022 Day 4 - Camp Cleanup - Elves need to clean up sections of the camp and they compare their assignments to find overlaps.
Problem🤔
Camp Cleanup: Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique ID number
, and each Elf is assigned a range of section IDs.
However, as some of the Elves compare their section assignments with each other, they’ve noticed that many of the assignments overlap. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a big list of the section assignments for each pair (your puzzle input).
Sample Data
For example, consider the following list of section assignment pairs:
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
For the first few pairs, this list means:
- Within the first pair of Elves, the first Elf was assigned sections
2-4
(sections2
,3
, and4
), while the second Elf was assigned sections6-8
(sections6
,7
,8
). - The Elves in the second pair were each assigned two sections.
- The Elves in the third pair were each assigned three sections: one got sections
5
,6
, and7
, while the other also got7
, plus8
and9
.
This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger numbers. Visually, these pairs of section assignments look like this:
.234..... 2-4
.....678. 6-8
.23...... 2-3
...45.... 4-5
....567.. 5-7
......789 7-9
.2345678. 2-8
..34567.. 3-7
.....6... 6-6
...456... 4-6
.23456... 2-6
...45678. 4-8
.234..... 2-4
.....678. 6-8
.23...... 2-3
...45.... 4-5
....567.. 5-7
......789 7-9
.2345678. 2-8
..34567.. 3-7
.....6... 6-6
...456... 4-6
.23456... 2-6
...45678. 4-8
Part 1
Some of the pairs have noticed that one of their assignments fully contains the other. For example, 2-8
fully contains 3-7
, and 6-6
is fully contained by 4-6
. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are 2 such pairs.
In how many assignment pairs does one range fully contain the other?
Solution 1💡
import { input } from './input.ts';
// Split the input into an array of pairs
// each pair being an array of 4 numbers
const pairs = input
// Split the input into an array of strings
// one string per pair
.split('\n')
.map((pair) =>
// Split each pair string into an array of two strings
pair.split(',').flatMap((range) =>
// Split each range string into an array of two numbers
range
.split('-')
// Convert the strings to numbers
.map((digit) => +digit)
)
);
// Initialize a counter to 0
let count = 0;
// Loop through each pair
pairs.forEach((pr) => {
// Destructure the array of 4 numbers into 4 separate variables
const [E1S, E1E, E2S, E2E] = pr;
// Check if the first Elf's range fully contains the second Elf's range
const E1_contains_E2 = E1S <= E2S && E2E <= E1E;
// Check if the second Elf's range fully contains the first Elf's range
const E2_contains_E1 = E2S <= E1S && E1E <= E2E;
// If either of the above conditions are true, increment the counter
if (E1_contains_E2 || E2_contains_E1) {
count++;
}
});
// Print the final count
console.log(count);
import { input } from './input.ts';
// Split the input into an array of pairs
// each pair being an array of 4 numbers
const pairs = input
// Split the input into an array of strings
// one string per pair
.split('\n')
.map((pair) =>
// Split each pair string into an array of two strings
pair.split(',').flatMap((range) =>
// Split each range string into an array of two numbers
range
.split('-')
// Convert the strings to numbers
.map((digit) => +digit)
)
);
// Initialize a counter to 0
let count = 0;
// Loop through each pair
pairs.forEach((pr) => {
// Destructure the array of 4 numbers into 4 separate variables
const [E1S, E1E, E2S, E2E] = pr;
// Check if the first Elf's range fully contains the second Elf's range
const E1_contains_E2 = E1S <= E2S && E2E <= E1E;
// Check if the second Elf's range fully contains the first Elf's range
const E2_contains_E1 = E2S <= E1S && E1E <= E2E;
// If either of the above conditions are true, increment the counter
if (E1_contains_E2 || E2_contains_E1) {
count++;
}
});
// Print the final count
console.log(count);
First, lets split the input into an array of pairs, with each pair being an array of 4 numbers.
Then, we initialize a counter to 0.
Next, we can loop through each pair and destructures the array of 4 numbers into 4 separate variables. Then check if the first Elf’s range fully contains the second Elf’s range, or if the second Elf’s range fully contains the first Elf’s range. If either of these conditions are true, we increment the counter.
Finally, print the final count.
Part 2
It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all.
In the above example, the first two pairs (2-4
,6-8
and 2-3
,4-5
) don’t overlap, while the remaining four pairs (5-7
,7-9
, 2-8
,3-7
, 6-6
,4-6
, and 2-6
,4-8
) do overlap:
5-7
,7-9
overlaps in a single section,7
.2-8
,3-7
overlaps all of the sections3
through7
.6-6
,4-6
overlaps in a single section,6
.2-6
,4-8
overlaps in sections4
,5
, and6
. So, in this example, the number of overlapping assignment pairs is4
.
In how many assignment pairs do the ranges overlap?
Solution 2💡
import { input } from './input.ts';
const pairs = input
.split('\n')
.map((pair) =>
pair
.split(',')
.flatMap((range) => range.split('-').map((digit) => +digit))
);
let count = 0;
pairs.forEach((pr) => {
// Destructure the array of 4
// numbers into 4 separate variables
const [_1S, _1E, _2S, _2E] = pr;
// Check if the start of the first
// range is inside the second range
const s1_2 = _2S <= _1S && _1S <= _2E;
// Check if the end of the first
// range is inside the second range
const e1_2 = _2S <= _1E && _1E <= _2E;
// Check if the start of the second
// range is inside the first range
const s2_1 = _1S <= _2S && _2S <= _1E;
// Check if the end of the second
// range is inside the first range
const e2_1 = _1S <= _2E && _2E <= _1E;
// If any of the above conditions
// are true, increment the counter
if (s1_2 || e1_2 || s2_1 || e2_1) {
count++;
}
});
// Print the final count
console.log(count);
import { input } from './input.ts';
const pairs = input
.split('\n')
.map((pair) =>
pair
.split(',')
.flatMap((range) => range.split('-').map((digit) => +digit))
);
let count = 0;
pairs.forEach((pr) => {
// Destructure the array of 4
// numbers into 4 separate variables
const [_1S, _1E, _2S, _2E] = pr;
// Check if the start of the first
// range is inside the second range
const s1_2 = _2S <= _1S && _1S <= _2E;
// Check if the end of the first
// range is inside the second range
const e1_2 = _2S <= _1E && _1E <= _2E;
// Check if the start of the second
// range is inside the first range
const s2_1 = _1S <= _2S && _2S <= _1E;
// Check if the end of the second
// range is inside the first range
const e2_1 = _1S <= _2E && _2E <= _1E;
// If any of the above conditions
// are true, increment the counter
if (s1_2 || e1_2 || s2_1 || e2_1) {
count++;
}
});
// Print the final count
console.log(count);
The code takes in an input string containing multiple pairs of ranges in the format 1-2,3-4
. It then splits the input into an array of pairs, where each pair is an array of four numbers: [1, 2, 3, 4]
.
Next, it initializes a counter to 0
and loops through each pair. For each pair, it destructures the array of four numbers into four separate variables: _1S
, _1E
, _2S
, and _2E
.
It then checks if the start or end of the first range overlaps the second range, or if the start or end of the second range overlaps the first range. If any of these conditions are true, it increments the counter.
Finally, it prints the final count.
Links to Keywords🔗
split(): This is a method of the String object in JavaScript and TypeScript. It splits a string into an array of substrings by separating the string at a specified delimiter.
map(): This is a method of the Array object in JavaScript and TypeScript. It creates a new array by applying a function to each element of the original array.
flatMap(): This is a method of the Array object in JavaScript and TypeScript. It is similar to map(), but it flattens the resulting array by one level. In other words, if map() returns an array of arrays, flatMap() will return a single array with all the elements of the original arrays.
forEach(): This is a method of the Array object in JavaScript and TypeScript. It executes a callback function once for each element of the array.