Day 4 part 1

This commit is contained in:
Yash Karandikar 2022-12-04 14:56:44 -06:00
parent 315f56f4c0
commit 8717e1d31a
2 changed files with 1027 additions and 0 deletions

27
four.rs Normal file
View file

@ -0,0 +1,27 @@
fn main() {
let buf = std::fs::read_to_string("four.txt").unwrap();
let mut count = 0u32;
for line in buf.lines() {
let (first, last) = line.split_once(',').unwrap();
let (first_lower, first_upper) = first
.split_once('-')
.map(|(a, b)| (a.parse::<u32>().unwrap(), b.parse::<u32>().unwrap()))
.unwrap();
let (last_lower, last_upper) = last
.split_once('-')
.map(|(a, b)| (a.parse::<u32>().unwrap(), b.parse::<u32>().unwrap()))
.unwrap();
if contains(first_lower, first_upper, last_lower, last_upper)
|| contains(last_lower, last_upper, first_lower, first_upper)
{
count += 1;
}
}
println!("{}", count);
}
fn contains(a: u32, b: u32, c: u32, d: u32) -> bool {
return (a..=b).contains(&c) && (a..=b).contains(&d);
}

1000
four.txt Normal file

File diff suppressed because it is too large Load diff