day_2_2
functionnal thingy thingy
This commit is contained in:
parent
892290d1d0
commit
f27bbdc4ae
3 changed files with 48 additions and 4 deletions
|
@ -3,7 +3,6 @@ use std::fs;
|
|||
pub fn solve_day_2_1() {
|
||||
let file_path = "/home/clement/RustroverProjects/advent_of_code/src/inputs/input_2.txt";
|
||||
println!("In file {file_path}");
|
||||
|
||||
let contents = fs::read_to_string(file_path)
|
||||
.expect("Something went wrong reading the file")
|
||||
.split("\n")
|
||||
|
|
42
src/day_2_2.rs
Normal file
42
src/day_2_2.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
pub fn is_safe(level : &Vec<i32>, skip_i : usize) -> bool
|
||||
{
|
||||
let level_without_skip_i = level
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(i, _)| *i != skip_i)
|
||||
.map(|(_, x)| *x)
|
||||
.collect::<Vec<i32>>();
|
||||
|
||||
|
||||
let should_increase = level_without_skip_i[1] > level_without_skip_i[0];
|
||||
|
||||
level_without_skip_i.windows(2).all(|win| (
|
||||
win[1] - win[0]).abs() <= 3 &&
|
||||
(win[1] > win[0]) == should_increase &&
|
||||
win[1] != win[0])
|
||||
}
|
||||
pub fn solve_day_2_2() {
|
||||
let file_path = "/home/clement/RustroverProjects/advent_of_code/src/inputs/input_2.txt";
|
||||
println!("In file {file_path}");
|
||||
|
||||
let binding = fs::read_to_string(file_path)
|
||||
.expect("Something went wrong reading the file");
|
||||
|
||||
let contents = binding
|
||||
.split("\n")
|
||||
.map(|x|x.split(" ").map(|y| y.parse::<i32>().unwrap()).collect::<Vec<i32>>());
|
||||
|
||||
let good_levels = contents.map(
|
||||
|level| {
|
||||
(0..(level.len()+1)).rev().any(|i| is_safe(&level, i))
|
||||
}
|
||||
);
|
||||
|
||||
let ans = good_levels.filter(|x| *x).count();
|
||||
|
||||
println!("{:?}", ans);
|
||||
|
||||
}
|
|
@ -1,14 +1,17 @@
|
|||
// mod day_1_1;
|
||||
// mod day_1_2;
|
||||
mod day_2_1;
|
||||
mod day_2_2;
|
||||
|
||||
// use crate::day_1_1::solve_day_1_1;
|
||||
// use crate::day_1_2::solve_day_1_2;
|
||||
use crate::day_2_1::solve_day_2_1;
|
||||
//use crate::day_2_1::solve_day_2_1;
|
||||
use crate::day_2_2::solve_day_2_2;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
// solve_day_1_1();
|
||||
//solve_day_1_1();
|
||||
//solve_day_1_2();
|
||||
solve_day_2_1();
|
||||
//solve_day_2_1();
|
||||
solve_day_2_2();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue