master
george 2 years ago
parent 2dc8abfbc6
commit 70894fa3a8

3
.gitignore vendored

@ -1,4 +1,3 @@
target/
input
testinput
*input
main

7
day04/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day04"
version = "0.1.0"

@ -0,0 +1,8 @@
[package]
name = "day04"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,59 @@
use std::io::{self, BufRead};
use std::num::ParseIntError;
use std::str::FromStr;
#[derive(Debug)]
struct Pair {
first: (usize, usize),
second: (usize, usize),
}
fn main() {
let pairs = io::stdin()
.lock()
.lines()
.map(|l| Pair::from_str(l.unwrap().as_str()).unwrap());
let mut full_overlap = 0;
let mut partial_overlap = 0;
for pair in pairs {
if (pair.first.0 >= pair.second.0 && pair.first.1 <= pair.second.1)
|| (pair.first.0 <= pair.second.0 && pair.first.1 >= pair.second.1)
{
full_overlap += 1;
}
if (pair.first.0 >= pair.second.0 && pair.first.0 <= pair.second.1)
|| (pair.first.1 >= pair.second.0 && pair.first.1 <= pair.second.1)
|| (pair.second.0 >= pair.first.0 && pair.second.0 <= pair.first.1)
|| (pair.second.1 >= pair.first.0 && pair.second.1 <= pair.first.1)
{
partial_overlap += 1;
}
}
println!("P1: {}, P2: {}", full_overlap, partial_overlap);
}
impl FromStr for Pair {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (first, second) = s.split_once(',').unwrap();
let first_half = first.split_once('-').unwrap();
let first_half = (
first_half.0.parse::<usize>().unwrap(),
first_half.1.parse::<usize>().unwrap(),
);
let second_half = second.split_once('-').unwrap();
let second_half = (
second_half.0.parse::<usize>().unwrap(),
second_half.1.parse::<usize>().unwrap(),
);
Ok(Pair {
first: first_half,
second: second_half,
})
}
}
Loading…
Cancel
Save