master
kalle 2022-12-04 23:29:55 +01:00
parent 60af8bb695
commit 394e3c175c
3 changed files with 1082 additions and 0 deletions

1000
input/04.txt Normal file

File diff suppressed because it is too large Load Diff

41
src/bin/04_1.rs Normal file
View File

@ -0,0 +1,41 @@
use std::fs;
use anyhow::Result;
use itertools::Itertools;
fn main() -> Result<()> {
let input = fs::read_to_string("input/04.txt")?;
let lines: Vec<&str> = input
.split("\n")
.filter(|s| *s != "")
.collect_vec();
// ........fl........fu......
// ......sl............su....
//
// ..fl.....fu...............
// ...........sl....su.......
//
// ........fl........fu......
// ..........sl....su........
//
// ................fl....fu..
// ..sl......su..............
let result = lines
.into_iter()
.map(|s| s.split(","))
.map(|mut i| (i.next().unwrap(), i.next().unwrap()))
.map(|(f, s)| (f.split("-"), s.split("-")))
.map(|(mut f, mut s)| (f.next().unwrap().parse::<i32>().ok(), f.next().unwrap().parse::<i32>().ok(), s.next().unwrap().parse::<i32>().ok(), s.next().unwrap().parse::<i32>().ok()))
.filter(|(fl, fu, sl, su)| (fl >= sl && fu <= su && fu >= sl) || (fl <= sl && fu >= su && fl <= su))
.count();
println!("{result}");
return Ok(());
}

41
src/bin/04_2.rs Normal file
View File

@ -0,0 +1,41 @@
use std::fs;
use anyhow::Result;
use itertools::Itertools;
fn main() -> Result<()> {
let input = fs::read_to_string("input/04.txt")?;
let lines: Vec<&str> = input
.split("\n")
.filter(|s| *s != "")
.collect_vec();
// ........fl........fu......
// ......sl............su....
//
// ..fl.....fu...............
// .......sl....su.........
//
// ........fl........fu......
// ..........sl....su........
//
// ........fl....fu..........
// ..sl......su..............
let result = lines
.into_iter()
.map(|s| s.split(","))
.map(|mut i| (i.next().unwrap(), i.next().unwrap()))
.map(|(f, s)| (f.split("-"), s.split("-")))
.map(|(mut f, mut s)| (f.next().unwrap().parse::<i32>().ok(), f.next().unwrap().parse::<i32>().ok(), s.next().unwrap().parse::<i32>().ok(), s.next().unwrap().parse::<i32>().ok()))
.filter(|(fl, fu, sl, su)| (fl >= sl && fl <= su) || (fu >= sl && fu <= su ) || (sl >= fl && sl <= fu) || (su >= fl && su <= fu))
.count();
println!("{result}");
return Ok(());
}