Day 4 puzzle

master
Wynd 2022-12-04 12:23:54 +02:00
parent 6ab8ce9f81
commit b2fed2d674
6 changed files with 1073 additions and 4 deletions

5
Cargo.lock generated
View File

@ -9,6 +9,7 @@ dependencies = [
"day1",
"day2",
"day3",
"day4",
]
[[package]]
@ -22,3 +23,7 @@ version = "0.1.0"
[[package]]
name = "day3"
version = "0.1.0"
[[package]]
name = "day4"
version = "0.1.0"

View File

@ -9,10 +9,12 @@ edition = "2021"
members = [
"day1",
"day2",
"day3"
"day3",
"day4"
]
[dependencies]
day1 = { path = "./day1" }
day2 = { path = "./day2" }
day3 = { path = "./day3" }
day3 = { path = "./day3" }
day4 = { path = "./day4" }

8
day4/Cargo.toml 100644
View File

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

1000
day4/src/input.txt 100644

File diff suppressed because it is too large Load Diff

53
day4/src/lib.rs 100644
View File

@ -0,0 +1,53 @@
use std::{fs, ops::Range};
pub fn start() {
let input = fs::read_to_string("./day4/src/input.txt").unwrap();
let mut sum1 = 0;
let mut sum2 = 0;
input
.lines()
.map(|f| {
f.split(",")
.collect::<Vec<&str>>()
.iter()
.map(|f| {
f.split("-")
.map(|f| f.parse::<u32>().unwrap())
.collect::<Vec<u32>>()
})
.collect::<Vec<Vec<u32>>>()
})
.for_each(|f| {
let i1 = f.get(0).unwrap();
let i2 = f.get(1).unwrap();
let r1 = Range {
start: i1.get(0).unwrap().to_owned(),
end: i1.get(1).unwrap().to_owned(),
};
let r2 = Range {
start: i2.get(0).unwrap().to_owned(),
end: i2.get(1).unwrap().to_owned(),
};
if (r1.start >= r2.start && r1.end <= r2.end)
|| (r2.start >= r1.start && r2.end <= r1.end)
{
sum1 += 1;
}
if r1.start == r2.start
|| r1.end == r2.end
|| r1.contains(&r2.start)
|| r1.contains(&r2.end)
|| r2.contains(&r1.start)
|| r2.contains(&r1.end)
{
sum2 += 1;
}
});
println!("Part 1: {:#?}", sum1);
println!("Part 2: {:#?}", sum2)
}

View File

@ -1,9 +1,10 @@
use day1;
use day2;
use day3;
use day4;
fn main() {
// day1::start();
// day2::start();
day3::start();
// day3::start();
day4::start();
}