day 2
This commit is contained in:
parent
ac7a676033
commit
db5983659f
3 changed files with 97 additions and 0 deletions
7
2024/day2/part2/Cargo.lock
generated
Normal file
7
2024/day2/part2/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 = "part1"
|
||||||
|
version = "0.1.0"
|
6
2024/day2/part2/Cargo.toml
Normal file
6
2024/day2/part2/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "part1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
84
2024/day2/part2/src/main.rs
Normal file
84
2024/day2/part2/src/main.rs
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
use std::thread::current;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// read file input
|
||||||
|
let lines: Vec<String> = read_to_string("input.txt").unwrap().lines().map(String::from).collect();
|
||||||
|
// current safe total count
|
||||||
|
let mut safecount = 0;
|
||||||
|
// for each line
|
||||||
|
for i in lines {
|
||||||
|
|
||||||
|
println!("{i}");
|
||||||
|
// for each line, split into a Vec<String> of the values
|
||||||
|
let mut frfrstr: Vec<String> = i.split_whitespace().map(|s| s.parse().unwrap()).collect();
|
||||||
|
// convert that Vec<String> into Vec<Int>
|
||||||
|
let mut frfrfr: Vec<i64> = Vec::new();
|
||||||
|
for j1 in frfrstr {
|
||||||
|
let j2: i64 = j1.parse::<i64>().unwrap();
|
||||||
|
frfrfr.push(j2);
|
||||||
|
}
|
||||||
|
// for each line, check if safe. Start false. If it ends up being fine then it'll be set to true.
|
||||||
|
let mut safemini = false;
|
||||||
|
|
||||||
|
for i in 0..frfrfr.len() {
|
||||||
|
let mut frfr: Vec<i64> = frfrfr.clone();
|
||||||
|
if i != frfr.len() {
|
||||||
|
frfr.remove(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// sorted regular and reverse to check condition 1
|
||||||
|
let mut sort1 = frfr.clone();
|
||||||
|
sort1.sort();
|
||||||
|
let mut sort2 = sort1.clone();
|
||||||
|
sort2.reverse();
|
||||||
|
if frfr == sort1 || frfr == sort2 {
|
||||||
|
let mut mode = 0;
|
||||||
|
let tempval0 = frfr[*&0];
|
||||||
|
let tempval1 = frfr[*&1];
|
||||||
|
if tempval0 < tempval1 {
|
||||||
|
println!("increasing mode");
|
||||||
|
mode = 1;
|
||||||
|
} else if tempval0 > tempval1 {
|
||||||
|
println!("decreasing mode");
|
||||||
|
mode = -1;
|
||||||
|
} else if tempval0 == tempval1 {
|
||||||
|
println!("its equal and so its discarded");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// condition 2 but only if cond 1 passes
|
||||||
|
// starts true, becomes invalid
|
||||||
|
let mut currentcheckstate = true;
|
||||||
|
'fr1: for i2 in 1..frfr.len() {
|
||||||
|
let realdiff = frfr[*&i2 - 1] - (frfr[*&i2]);
|
||||||
|
if mode == 1 {
|
||||||
|
if realdiff > -1 || realdiff < -3 {
|
||||||
|
println!("realdiff {realdiff} plus");
|
||||||
|
currentcheckstate = false;
|
||||||
|
println!("abs diff averted");
|
||||||
|
}
|
||||||
|
} else if mode == -1 {
|
||||||
|
if realdiff > 3 || realdiff < 1 {
|
||||||
|
println!("realdiff {realdiff} minus");
|
||||||
|
currentcheckstate = false;
|
||||||
|
println!("abs diff averted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if currentcheckstate {
|
||||||
|
safemini = true;
|
||||||
|
println!("allowed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if safemini {
|
||||||
|
safecount += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
println!("{safecount}")
|
||||||
|
}
|
Loading…
Reference in a new issue