Day 4 done
This commit is contained in:
parent
dbf91b37d8
commit
f67370b26d
|
@ -5,3 +5,21 @@ version = 3
|
|||
[[package]]
|
||||
name = "AoC2022"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.10.5"
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,53 @@
|
|||
use itertools::Itertools;
|
||||
use std::collections::HashSet;
|
||||
use std::error::Error;
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::vec::Vec;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let file = File::open("inputs/day4.txt")?;
|
||||
let lines = io::BufReader::new(file).lines().map(|l| l.unwrap());
|
||||
|
||||
let sections: Vec<((u32, u32), (u32, u32))> = lines
|
||||
.map(|l| {
|
||||
l.split(',')
|
||||
.map(|p| {
|
||||
p.split('-')
|
||||
.map(|id| id.parse::<u32>().unwrap())
|
||||
.collect_tuple()
|
||||
.unwrap()
|
||||
})
|
||||
.collect_tuple()
|
||||
.unwrap()
|
||||
})
|
||||
.collect();
|
||||
|
||||
let count1 = sections
|
||||
.iter()
|
||||
.map(|((a, b), (x, y))| {
|
||||
if (a >= x && b <= y) || (x >= a && y <= b) {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
})
|
||||
.fold(0, |a, b| a + b);
|
||||
|
||||
println!("Answer Part1: {}", count1);
|
||||
|
||||
let count2 = sections
|
||||
.iter()
|
||||
.map(|((a, b), (x, y))| {
|
||||
if (a <= x && x <= b) || (x <= a && a <= y) {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
}
|
||||
})
|
||||
.fold(0, |a, b| a + b);
|
||||
|
||||
println!("Answer Part2: {}", count2);
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue