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

View file

@ -1,7 +1,6 @@
use std::fs; 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 let level_without_skip_i = level
.iter() .iter()
.enumerate() .enumerate()
@ -9,33 +8,27 @@ pub fn is_safe(level : &Vec<i32>, skip_i : usize) -> bool
.map(|(_, x)| *x) .map(|(_, x)| *x)
.collect::<Vec<i32>>(); .collect::<Vec<i32>>();
let should_increase = level_without_skip_i[1] > level_without_skip_i[0]; let should_increase = level_without_skip_i[1] > level_without_skip_i[0];
level_without_skip_i.windows(2).all(|win| ( level_without_skip_i.windows(2).all(|win| {
win[1] - win[0]).abs() <= 3 && (win[1] - win[0]).abs() <= 3 && (win[1] > win[0]) == should_increase && win[1] != win[0]
(win[1] > win[0]) == should_increase && })
win[1] != win[0])
} }
pub fn solve_day_2_2() { pub fn solve_day_2_2() {
let file_path = "/home/clement/RustroverProjects/advent_of_code/src/inputs/input_2.txt"; let file_path = "/home/clement/RustroverProjects/advent_of_code/src/inputs/input_2.txt";
println!("In file {file_path}"); println!("In file {file_path}");
let binding = fs::read_to_string(file_path) let binding = fs::read_to_string(file_path).expect("Something went wrong reading the file");
.expect("Something went wrong reading the file");
let contents = binding let contents = binding.lines().map(|x| {
.split("\n") x.split_whitespace()
.map(|x|x.split(" ").map(|y| y.parse::<i32>().unwrap()).collect::<Vec<i32>>()); .map(|y| y.parse::<i32>().unwrap())
.collect::<Vec<i32>>()
});
let good_levels = contents.map( let good_levels = contents.map(|level| (0..=level.len()).rev().any(|i| is_safe(&level, i)));
|level| {
(0..(level.len()+1)).rev().any(|i| is_safe(&level, i))
}
);
let ans = good_levels.filter(|x| *x).count(); let ans = good_levels.filter(|x| *x).count();
println!("{:?}", ans); println!("{:?}", ans);
} }