mirror of
https://github.com/anthonyoteri/advent-of-code-2022.git
synced 2026-06-05 17:46:54 -04:00
Solution for Day 4
This commit is contained in:
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day4"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day4"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,6 @@
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
||||
+1000
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
--- Day 4: 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).
|
||||
|
||||
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
|
||||
For the first few pairs, this list means:
|
||||
|
||||
Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and 4), while the second Elf was assigned sections 6-8 (sections 6, 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, and 7, while the other also got 7, plus 8 and 9.
|
||||
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
|
||||
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?
|
||||
|
||||
Your puzzle answer was 515.
|
||||
|
||||
--- Part Two ---
|
||||
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 sections 3 through 7.
|
||||
6-6,4-6 overlaps in a single section, 6.
|
||||
2-6,4-8 overlaps in sections 4, 5, and 6.
|
||||
So, in this example, the number of overlapping assignment pairs is 4.
|
||||
|
||||
In how many assignment pairs do the ranges overlap?
|
||||
|
||||
Your puzzle answer was 883.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, you should return to your Advent calendar and try another puzzle.
|
||||
|
||||
If you still want to see it, you can get your puzzle input.
|
||||
|
||||
*/
|
||||
use std::collections::HashSet;
|
||||
use std::error::Error;
|
||||
|
||||
fn expand_range(range_str: &str) -> Result<HashSet<u32>, Box<dyn Error>> {
|
||||
let parts = range_str.split_once('-').unwrap();
|
||||
|
||||
let begin: u32 = parts.0.parse()?;
|
||||
let end: u32 = parts.1.parse()?;
|
||||
|
||||
Ok((begin..=end).collect())
|
||||
}
|
||||
|
||||
fn parse_line(line: &str) -> Result<(HashSet<u32>, HashSet<u32>), Box<dyn Error>> {
|
||||
let splits = line.split_once(',').unwrap();
|
||||
Ok((expand_range(splits.0)?, expand_range(splits.1)?))
|
||||
}
|
||||
|
||||
fn find_intersection(s1: &HashSet<u32>, s2: &HashSet<u32>) -> HashSet<u32> {
|
||||
s1.intersection(s2).copied().collect()
|
||||
}
|
||||
|
||||
fn fully_contains(s1: &HashSet<u32>, s2: &HashSet<u32>) -> bool {
|
||||
let isect = find_intersection(s1, s2);
|
||||
isect == *s1 || isect == *s2
|
||||
}
|
||||
|
||||
fn part1() -> usize {
|
||||
let input = include_str!("../data/input.txt");
|
||||
input
|
||||
.lines()
|
||||
.map(|l| parse_line(l).unwrap())
|
||||
.filter(|(l, r)| fully_contains(l, r))
|
||||
.count()
|
||||
}
|
||||
|
||||
fn part2() -> usize {
|
||||
let input = include_str!("../data/input.txt");
|
||||
input
|
||||
.lines()
|
||||
.map(|l| parse_line(l).unwrap())
|
||||
.filter(|(l, r)| !find_intersection(l, r).is_empty())
|
||||
.count()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Part1 answer {}", part1());
|
||||
println!("Part2 answer {}", part2());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse_line() -> Result<(), Box<dyn Error>> {
|
||||
let input = "2-4,6-8";
|
||||
assert_eq!(
|
||||
parse_line(input)?,
|
||||
(HashSet::from([2, 3, 4]), HashSet::from([6, 7, 8]))
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_intersection() {
|
||||
let lhs = HashSet::from([1, 2, 3]);
|
||||
let rhs = HashSet::from([2, 3, 4]);
|
||||
let expected = HashSet::from([2, 3]);
|
||||
|
||||
assert_eq!(find_intersection(&lhs, &rhs), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fully_contains() {
|
||||
let lhs = HashSet::from([2, 3, 4, 5]);
|
||||
let rhs = HashSet::from([3, 4]);
|
||||
|
||||
assert!(fully_contains(&lhs, &rhs));
|
||||
assert!(fully_contains(&rhs, &lhs));
|
||||
|
||||
let rhs = HashSet::from([4, 5, 6]);
|
||||
assert!(!fully_contains(&lhs, &rhs));
|
||||
assert!(!fully_contains(&rhs, &lhs));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_1() {
|
||||
let input = include_str!("../data/input-test.txt");
|
||||
let result = input
|
||||
.lines()
|
||||
.map(|l| parse_line(l).unwrap())
|
||||
.filter(|(l, r)| fully_contains(l, r))
|
||||
.count();
|
||||
assert_eq!(result, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_2() {
|
||||
let input = include_str!("../data/input-test.txt");
|
||||
let result = input
|
||||
.lines()
|
||||
.map(|l| parse_line(l).unwrap())
|
||||
.filter(|(l, r)| !find_intersection(l, r).is_empty())
|
||||
.count();
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user