2022-12-05 00:15:36 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
use std::io::BufRead;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let now = Instant::now();
|
|
|
|
part_one();
|
|
|
|
let part_one_duration = now.elapsed();
|
|
|
|
part_two();
|
|
|
|
let part_two_duration = now.elapsed();
|
|
|
|
println!(
|
|
|
|
"p1: {}ms, p2: {}ms",
|
|
|
|
part_one_duration.as_millis(),
|
|
|
|
(part_two_duration - part_one_duration).as_millis()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
|
|
|
where
|
|
|
|
P: AsRef<Path>,
|
|
|
|
{
|
|
|
|
// note that this discards a final newline
|
|
|
|
let file = File::open(filename)?;
|
|
|
|
Ok(io::BufReader::new(file).lines())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn part_one() {
|
|
|
|
if let Ok(lines) = read_lines("./inputs/input") {
|
|
|
|
let mut overlaps = 0;
|
|
|
|
for line in lines {
|
|
|
|
if let Ok(ranges) = line {
|
2022-12-05 00:25:13 +00:00
|
|
|
let v: Vec<Vec<i8>> = ranges
|
2022-12-05 00:15:36 +00:00
|
|
|
.split(",")
|
2022-12-05 00:25:13 +00:00
|
|
|
.map(|r| r.split("-").map(|i| i.parse::<i8>().unwrap()).collect())
|
2022-12-05 00:15:36 +00:00
|
|
|
.collect();
|
|
|
|
// 00 ... 01
|
|
|
|
// 10 ... 11
|
2022-12-05 00:25:13 +00:00
|
|
|
if (v[0][0] <= v[1][0] && v[0][1] >= v[1][1])
|
|
|
|
|| (v[1][0] <= v[0][0] && v[1][1] >= v[0][1])
|
2022-12-05 00:15:36 +00:00
|
|
|
{
|
|
|
|
overlaps += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("{}", overlaps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn part_two() {
|
|
|
|
if let Ok(lines) = read_lines("./inputs/input") {
|
|
|
|
let mut overlaps = 0;
|
|
|
|
for line in lines {
|
|
|
|
if let Ok(ranges) = line {
|
2022-12-05 00:25:13 +00:00
|
|
|
let v: Vec<Vec<i8>> = ranges
|
2022-12-05 00:15:36 +00:00
|
|
|
.split(",")
|
2022-12-05 00:25:13 +00:00
|
|
|
.map(|r| r.split("-").map(|i| i.parse::<i8>().unwrap()).collect())
|
2022-12-05 00:15:36 +00:00
|
|
|
.collect();
|
|
|
|
// 00 ... 01
|
|
|
|
// 10 ... 11
|
|
|
|
// uuuuuugh
|
2022-12-05 00:25:13 +00:00
|
|
|
if (v[0][0] <= v[1][0] && v[0][1] >= v[1][1])
|
|
|
|
|| (v[1][0] <= v[0][0] && v[1][1] >= v[0][1])
|
|
|
|
|| (v[0][0] <= v[1][1] && v[0][0] >= v[1][0])
|
|
|
|
|| (v[0][1] >= v[1][0] && v[0][1] <= v[1][1])
|
2022-12-05 00:15:36 +00:00
|
|
|
{
|
|
|
|
overlaps += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("{}", overlaps);
|
|
|
|
}
|
|
|
|
}
|