rustfmt + other things

This commit is contained in:
Crizomb 2024-12-08 23:09:33 +01:00
parent 3f742ab5ce
commit 8c19bc6597
2 changed files with 24 additions and 30 deletions

View file

@ -7,26 +7,27 @@ pub fn solve_day_2_1() {
.expect("Something went wrong reading the file")
.split("\n")
.map(|x| {
x.split(" ").map(|y| y.parse::<i32>().unwrap()).collect::<Vec<i32>>()
}).collect::<Vec<Vec<i32>>>();
x.split(" ")
.map(|y| y.parse::<i32>().unwrap())
.collect::<Vec<i32>>()
})
.collect::<Vec<Vec<i32>>>();
let first_diff_sign = contents
.iter()
.map(|vec| vec[1] - vec[0] > 0);
let first_diff_sign = contents.iter().map(|vec| vec[1] - vec[0] > 0);
let is_safe_list = contents
.iter()
.map(|vec| vec.windows(2))
.zip(first_diff_sign)
.map(|(mut vec_win, should_increase)| {
vec_win.all(|win| (
win[1] - win[0]).abs() <= 3 &&
(win[1] - win[0] > 0) == should_increase &&
win[1] != win[0])
vec_win.all(|win| {
(win[1] - win[0]).abs() <= 3
&& (win[1] - win[0] > 0) == should_increase
&& win[1] != win[0]
})
});
let ans = is_safe_list.filter(|x| *x).count();
println!("{:?}", ans);
}
}

View file

@ -1,7 +1,6 @@
use std::fs;
pub fn is_safe(level : &Vec<i32>, skip_i : usize) -> bool
{
pub fn is_safe(level: &Vec<i32>, skip_i: usize) -> bool {
let level_without_skip_i = level
.iter()
.enumerate()
@ -9,33 +8,27 @@ pub fn is_safe(level : &Vec<i32>, skip_i : usize) -> bool
.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])
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 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 contents = binding.lines().map(|x| {
x.split_whitespace()
.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 good_levels = contents.map(|level| (0..=level.len()).rev().any(|i| is_safe(&level, i)));
let ans = good_levels.filter(|x| *x).count();
println!("{:?}", ans);
}
}