commiting day4

This commit is contained in:
Dylan "smellyfis" Thies
2022-12-12 15:57:38 -05:00
parent 5522d57c90
commit 4ef68d5f36
4 changed files with 1064 additions and 0 deletions

7
day4/Cargo.lock generated Normal file
View File

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

8
day4/Cargo.toml Normal file
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/input Normal file

File diff suppressed because it is too large Load Diff

49
day4/src/main.rs Normal file
View File

@@ -0,0 +1,49 @@
use std::fs::File;
use std::io::{prelude::*, BufReader};
fn main() -> std::io::Result<()> {
//Read in file
let file = File::open("input")?;
let reader = BufReader::new(file);
let value = reader
.lines()
.map(|line| {
let line = line.unwrap();
// split and parse
let (a, b) = match line.split(',').take(2).collect::<Vec<&str>>()[..] {
[a, b] => (
a.split('-')
.take(2)
.map(|x| x.parse::<i32>().unwrap())
.collect::<Vec<i32>>(),
b.split('-')
.take(2)
.map(|x| x.parse::<i32>().unwrap())
.collect::<Vec<i32>>(),
),
_ => panic!("no good"),
};
(
// part 1 wholly overlapping
i32::from((a[0] <= b[0] && a[1] >= b[1]) || (a[0] >= b[0] && a[1] <= b[1])),
// part 2 any overlapping
i32::from(
(a[0] >= b[0] && a[0] <= b[1])
|| (a[1] >= b[0] && a[1] <= b[1])
|| (b[0] >= a[0] && b[0] <= a[1])
|| (b[1] >= a[0] && b[1] <= a[1]),
),
)
})
// using folding instead of sum() so that we can do both parts in one call
.fold((0, 0), |mut acc: (i32, i32), x| {
acc.0 += x.0;
acc.1 += x.1;
acc
});
println!("Part 1: {}", value.0);
println!("Part 2: {}", value.1);
Ok(())
}