Day 4, Part 1 & 2

This commit is contained in:
Aadi Desai 2023-10-13 11:34:05 +01:00
parent 9f6c7656d5
commit f1ed4e71df
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM
2 changed files with 1044 additions and 0 deletions

44
src/bin/d4.rs Normal file
View file

@ -0,0 +1,44 @@
const INPUT: &str = include_str!("../input/d4.txt");
fn main() -> anyhow::Result<()> {
let p1 = INPUT
.split('\n')
.map(Pair::from)
.filter(ranges_contain_each_other)
.count();
println!("{p1}");
let p2 = INPUT
.split('\n')
.map(Pair::from)
.filter(ranges_overlap)
.count();
println!("{p2}");
anyhow::Ok(())
}
struct Pair(usize, usize, usize, usize);
impl From<&str> for Pair {
fn from(line: &str) -> Self {
let v = line
.split(['-', ','])
.map(|s| {
s.parse::<usize>()
.expect("Failed to parse section ID as usize")
})
.collect::<Vec<_>>();
assert_eq!(v.len(), 4, "Expected 4 section IDs, line: {line}");
Pair(v[0], v[1], v[2], v[3])
}
}
fn ranges_contain_each_other(pair: &Pair) -> bool {
((pair.0 <= pair.2)
&& (pair.3 <= pair.1))
|| ((pair.2 <= pair.0)
&& (pair.1 <= pair.3))
}
fn ranges_overlap(pair: &Pair) -> bool {
!((pair.1 < pair.2) || (pair.3 < pair.0))
}

1000
src/input/d4.txt Normal file

File diff suppressed because it is too large Load diff