From ac7a676033516a954de55ef706c44df79c44f411 Mon Sep 17 00:00:00 2001 From: endernon Date: Mon, 2 Dec 2024 23:27:10 +0000 Subject: [PATCH] god damnit i can't believe i used == in place of = --- 2024/day2/part1/Cargo.lock | 7 ++++ 2024/day2/part1/Cargo.toml | 6 +++ 2024/day2/part1/src/main.rs | 82 +++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 2024/day2/part1/Cargo.lock create mode 100644 2024/day2/part1/Cargo.toml create mode 100644 2024/day2/part1/src/main.rs diff --git a/2024/day2/part1/Cargo.lock b/2024/day2/part1/Cargo.lock new file mode 100644 index 0000000..e73b84f --- /dev/null +++ b/2024/day2/part1/Cargo.lock @@ -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" diff --git a/2024/day2/part1/Cargo.toml b/2024/day2/part1/Cargo.toml new file mode 100644 index 0000000..d85b608 --- /dev/null +++ b/2024/day2/part1/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "part1" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/2024/day2/part1/src/main.rs b/2024/day2/part1/src/main.rs new file mode 100644 index 0000000..9e88f08 --- /dev/null +++ b/2024/day2/part1/src/main.rs @@ -0,0 +1,82 @@ +use std::fs::read_to_string; +use std::thread::current; + +fn main() { + // read file input + let lines: Vec = 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 of the values + let mut frfrstr: Vec = i.split_whitespace().map(|s| s.parse().unwrap()).collect(); + // convert that Vec into Vec + let mut frfr: Vec = Vec::new(); + for j1 in frfrstr { + let j2: i64 = j1.parse::().unwrap(); + frfr.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; + + // 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}") +}