day 2 1
This commit is contained in:
parent
4e8ad5a56d
commit
892290d1d0
4 changed files with 1041 additions and 7 deletions
|
@ -33,6 +33,4 @@ pub fn solve_day_1_2() {
|
|||
|
||||
println!("{similarity}");
|
||||
|
||||
|
||||
|
||||
}
|
33
src/day_2_1.rs
Normal file
33
src/day_2_1.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
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")
|
||||
.map(|x| {
|
||||
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 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])
|
||||
});
|
||||
|
||||
let ans = is_safe_list.filter(|x| *x).count();
|
||||
|
||||
println!("{:?}", ans);
|
||||
|
||||
}
|
1000
src/inputs/input_2.txt
Normal file
1000
src/inputs/input_2.txt
Normal file
File diff suppressed because it is too large
Load diff
13
src/main.rs
13
src/main.rs
|
@ -1,11 +1,14 @@
|
|||
mod day_1_1;
|
||||
mod day_1_2;
|
||||
// mod day_1_1;
|
||||
// mod day_1_2;
|
||||
mod day_2_1;
|
||||
|
||||
use crate::day_1_1::solve_day_1_1;
|
||||
use crate::day_1_2::solve_day_1_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;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
// solve_day_1_1();
|
||||
solve_day_1_2();
|
||||
//solve_day_1_2();
|
||||
solve_day_2_1();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue