added 2024 part 2
This commit is contained in:
1
2024/02/Rust/.gitignore
vendored
Normal file
1
2024/02/Rust/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
7
2024/02/Rust/Cargo.lock
generated
Normal file
7
2024/02/Rust/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day_2"
|
||||
version = "0.1.0"
|
||||
6
2024/02/Rust/Cargo.toml
Normal file
6
2024/02/Rust/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day_2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
6
2024/02/Rust/src/example.txt
Normal file
6
2024/02/Rust/src/example.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
1000
2024/02/Rust/src/input.txt
Normal file
1000
2024/02/Rust/src/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
64
2024/02/Rust/src/main.rs
Normal file
64
2024/02/Rust/src/main.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use std::{fs, process};
|
||||
|
||||
fn main() {
|
||||
let file_name = "./src/input.txt";
|
||||
let reports: Vec<Vec<i32>>;
|
||||
match fs::read_to_string(file_name) {
|
||||
Ok(data) => {
|
||||
reports = data
|
||||
.split("\r\n")
|
||||
.map(|report|
|
||||
report
|
||||
.split(" ")
|
||||
.map(|level| level
|
||||
.parse::<i32>().unwrap())
|
||||
.collect())
|
||||
.collect()
|
||||
},
|
||||
Err(error) => {
|
||||
println!("Failed to read file {file_name}: {error}");
|
||||
process::exit(1);
|
||||
},
|
||||
}
|
||||
let levels_per_report = reports
|
||||
.iter()
|
||||
.map(|report| {
|
||||
report
|
||||
.windows(2)
|
||||
.map(|slice| slice[0] - slice[1])
|
||||
.collect::<Vec<i32>>()
|
||||
})
|
||||
.collect::<Vec<Vec<i32>>>();
|
||||
let same_sign_reports = levels_per_report
|
||||
.iter()
|
||||
.map(|report| {
|
||||
report
|
||||
.iter()
|
||||
.skip(1)
|
||||
//.fold(init, f)
|
||||
.all(|level| (*level >= 0) == (*report.first().unwrap() >= 0))
|
||||
})
|
||||
.collect::<Vec<bool>>();
|
||||
let in_range_reports = levels_per_report
|
||||
.iter()
|
||||
.map(|report| {
|
||||
report
|
||||
.iter()
|
||||
.all(|level| level.abs() >= 1 && level.abs() <= 3)
|
||||
})
|
||||
.collect::<Vec<bool>>();
|
||||
let safe_reports = same_sign_reports
|
||||
.iter()
|
||||
.copied()
|
||||
.zip(in_range_reports.iter().copied())
|
||||
.map(|(l, r)| l && r)
|
||||
.fold(0, |acc, report| acc + report as i32);
|
||||
//.collect::<u32>();
|
||||
|
||||
//println!("Reports:\n{:?}", reports);
|
||||
//println!("Levels Per Report:\n{:?}", levels_per_report);
|
||||
//println!("Same Sign Reports:\n{:?}", same_sign_reports);
|
||||
//println!("In Range Reports:\n{:?}", in_range_reports);
|
||||
println!("Safe Reports:\n{:?}", safe_reports);
|
||||
process::exit(0)
|
||||
}
|
||||
Reference in New Issue
Block a user