initial commit
This commit is contained in:
commit
cf7d87c04d
34 changed files with 4282 additions and 0 deletions
1
day2/.gitignore
vendored
Normal file
1
day2/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target
|
||||
7
day2/Cargo.lock
generated
Normal file
7
day2/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day2"
|
||||
version = "0.1.0"
|
||||
6
day2/Cargo.toml
Normal file
6
day2/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "day2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
1000
day2/input
Normal file
1000
day2/input
Normal file
File diff suppressed because it is too large
Load diff
51
day2/src/main.rs
Normal file
51
day2/src/main.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let mut safe_reports = 0;
|
||||
let mut safe_reports_with_dampener = 0;
|
||||
if let Ok(lines) = read_lines("input") {
|
||||
for line in lines.flatten() {
|
||||
let report = line.trim().split(' ').flat_map(str::parse::<i32>).collect::<Vec<_>>();
|
||||
if check_report(&report) {
|
||||
safe_reports += 1;
|
||||
safe_reports_with_dampener += 1;
|
||||
}
|
||||
else {
|
||||
for i in 0..report.len() {
|
||||
let mut new_report = report.to_vec();
|
||||
new_report.remove(i);
|
||||
if check_report(&new_report) {
|
||||
safe_reports_with_dampener += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("Number of safe reports: {}", safe_reports);
|
||||
println!("Number of safe reports (with dampener): {}", safe_reports_with_dampener);
|
||||
}
|
||||
|
||||
fn check_report(report: &Vec<i32>) -> bool {
|
||||
let mut inc_or_dec:i32 = 0;
|
||||
for i in 0..report.len() - 1 {
|
||||
let diff = report[i+1] - report[i];
|
||||
println!("diff is {}", diff);
|
||||
if (inc_or_dec < 0 && diff > 0) || (inc_or_dec > 0 && diff < 0) {
|
||||
return false;
|
||||
}
|
||||
inc_or_dec = diff;
|
||||
if diff.abs() < 1 || diff.abs() > 3 {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where P: AsRef<Path>, {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue