advent_of_code_2022/aoc_04b/src/main.rs

38 lines
1.0 KiB
Rust

use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn main() {
let file_path = String::from("../aoc_04a/input");
let mut result = 0;
println!("In file {}", file_path);
if let Ok(lines) = read_lines(file_path) {
// Consumes the iterator, returns an (Optional) String
for line in lines {
if let Ok(ip) = line {
let split = ip.split(",");
let mut vec = Vec::<u32>::new();
for s in split {
let split = s.split("-");
for s in split {
vec.push(s.parse().unwrap());
}
}
if !(vec[1] < vec[2] || vec[0] > vec[3]) {
result += 1;
}
}
}
}
println!("{}", result);
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}