52 lines
1.1 KiB
Rust
52 lines
1.1 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 contains_partially(self: &Self, other: &Self) -> bool {
|
|
self.low <= other.high && self.high >= other.low
|
|
}
|
|
}
|
|
|
|
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_partially(&pairs[1])
|
|
})
|
|
.map(|b| if b { 1 } else { 0 })
|
|
.sum();
|
|
|
|
println!("{}", score);
|
|
}
|