Brute force go brrrrrrrrr

This commit is contained in:
Rolf Martin Glomsrud 2024-12-02 22:43:48 +01:00
parent a78026b746
commit e3d7517de3
3 changed files with 89 additions and 2 deletions

View file

@ -1,12 +1,14 @@
use one::One;
use runbase::RunBase;
use two::Two;
mod runbase;
mod one;
mod two;
fn main() {
let one = One::new();
let one = Two::new();
one.run();
}

View file

@ -1,6 +1,5 @@
use std::fs;
pub fn read_lines(file: &str) -> Vec<String>{
match fs::read_to_string(file){
Ok(x) => {

86
src/two.rs Normal file
View file

@ -0,0 +1,86 @@
use std::{collections::HashMap, hash::Hash};
use crate::runbase::{read_lines, RunBase};
pub struct Two {
lines: Vec<Vec<i32>>,
}
#[derive(Eq, PartialEq)]
enum State {
Increasing,
Decreason,
None,
}
impl Two {
fn single_level_test(line: &Vec<i32>) -> bool {
let mut state = State::None;
for (i, window) in line.as_slice().windows(2).enumerate() {
let diff = (window[0] - window[1]).abs();
if diff == 0 || diff > 3 {
return false
}
let mut tempState = State::None;
if window[0] < window[1] {
tempState = State::Increasing;
} else if window[0] > window[1] {
tempState = State::Decreason;
}
if state == tempState || state == State::None {
state = tempState;
} else {
return false;
}
}
return true
}
}
impl RunBase<Two> for Two {
fn new() -> Two {
return Two {
lines: read_lines("inputs/2")
.into_iter()
.map(|x| x.split(" ").map(|y| y.parse::<i32>().unwrap()).collect())
.collect(),
};
}
fn run(&self) {
self.run_1();
self.run_2();
}
fn run_1(&self) {
let mut count = 0;
let mut loopcount = 0;
for line in self.lines.iter() {
if Two::single_level_test(line) {
count += 1;
}
}
println!("Safe: {}, {}", count, loopcount);
}
fn run_2(&self) {
let mut count = 0;
let mut loopcount = 0;
for line in self.lines.iter() {
if Two::single_level_test(line) {
count += 1;
}else {
for i in 0..line.len(){
let mut line_clone = line.clone();
line_clone.remove(i);
if Two::single_level_test(&line_clone){
count += 1;
break;
}
}
}
//println!("{:?} is invalid", line);
}
println!("Safe: {}, {}", count, loopcount);
}
}