AOC2022/rust/4/part1.rs

49 lines
1.0 KiB
Rust

use std::{
fs::File,
io::{BufRead, BufReader},
};
struct Pair {
low: i32,
high: i32,
}
impl Pair {
fn new(input: &str) -> Self {
let tmpvec = input
.split('-')
.map(|val| val.parse::<i32>().unwrap())
.collect::<Vec<_>>();
return Self {
low: tmpvec[0],
high: tmpvec[1],
};
}
fn contains_fully(self: &Self, other: &Self) -> bool {
self.low <= other.low && self.high >= other.high
}
}
fn main() -> () {
let path = "input.txt";
let input = File::open(path).unwrap();
let buf = BufReader::new(input);
let score: i32 = buf
.lines()
.map(|line| {
let line = line.unwrap();
let pairs = line
.split(',')
.map(|pair_str| Pair::new(pair_str))
.collect::<Vec<_>>();
pairs[0].contains_fully(&pairs[1]) || pairs[1].contains_fully(&pairs[0])
})
.map(|b| if b { 1 } else { 0 })
.sum();
println!("{}", score);
}