From e1aa065f7ff41193f2212a8aee9577eb0bfb5a3b Mon Sep 17 00:00:00 2001 From: Aaro Koivukangas Date: Mon, 20 Jan 2025 12:21:00 +0200 Subject: [PATCH] 2024 day2? --- 2024/day2/Cargo.lock | 7 +++ 2024/day2/Cargo.toml | 8 +++ 2024/day2/src/main.rs | 111 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 2024/day2/Cargo.lock create mode 100644 2024/day2/Cargo.toml create mode 100644 2024/day2/src/main.rs diff --git a/2024/day2/Cargo.lock b/2024/day2/Cargo.lock new file mode 100644 index 0000000..63c2f60 --- /dev/null +++ b/2024/day2/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day2" +version = "0.1.0" diff --git a/2024/day2/Cargo.toml b/2024/day2/Cargo.toml new file mode 100644 index 0000000..8aa34bb --- /dev/null +++ b/2024/day2/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day2" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2024/day2/src/main.rs b/2024/day2/src/main.rs new file mode 100644 index 0000000..1555673 --- /dev/null +++ b/2024/day2/src/main.rs @@ -0,0 +1,111 @@ +use std::fs; + +fn main() { + println!("Hello, world!"); + let contents = parse_input_file("./input.txt"); + + let result1 = solve_part1(&contents); + println!("Result part1: {}", result1); + + let result2 = solve_part2(&contents); + println!("Result part2: {}", result2); +} + +fn count_results(reports: &Vec>, selector: &dyn Fn(&Vec, i32) -> bool) -> i32 { + let mut result: i32 = 0; + + for report in reports { + result += match selector(&report, 0) { + true => 1, + false => 0, + } + } + + result +} + +fn solve_part1(reports: &Vec>) -> i32 { + count_results(&reports, &is_monotonic_and_properly_gapped) +} + +fn solve_part2(reports: &Vec>) -> i32 { + count_results(&reports, &is_monotonic_and_properly_gapped_with_errors) +} + +fn is_monotonic_and_properly_gapped(line: &Vec, _depth: i32) -> bool { + let is_previous_increasing: bool = (line[0] - line[1]) > 0; + for idx in 0..(line.len() - 1) { + let difference: i32 = line[idx] - line[idx + 1]; + + if difference.abs() > 3 || 1 > difference.abs() { + return false; + } + + if difference > 0 && is_previous_increasing != true { + return false; + } + if difference < 0 && is_previous_increasing == true { + return false; + } + } + true +} + +fn is_monotonic_and_properly_gapped_with_errors(line: &Vec, depth: i32) -> bool { + if line.len() < 2 { + return false; + } + + let is_previous_increasing: bool = (line[0] - line[1]) > 0; + for idx in 0..(line.len() - 1) { + let difference: i32 = line[idx] - line[idx + 1]; + + if difference.abs() > 3 || 1 > difference.abs() { + for idx_i in 0..(line.len() - 1) { + let mut without_index: Vec = line.to_owned(); + without_index.remove(idx_i); + if depth >= 0 && is_monotonic_and_properly_gapped_with_errors(&without_index, depth - 1) { + return true; + }; + } + return false; + } + + if difference > 0 && is_previous_increasing != true { + for idx_i in 0..(line.len() - 1) { + let mut without_index: Vec = line.to_owned(); + without_index.remove(idx_i); + if depth >= 0 && is_monotonic_and_properly_gapped_with_errors(&without_index, depth - 1) { + return true; + }; + } + return false; + } + if difference < 0 && is_previous_increasing == true { + for idx_i in 0..(line.len() - 1) { + let mut without_index: Vec = line.to_owned(); + without_index.remove(idx_i); + if depth >= 0 && is_monotonic_and_properly_gapped_with_errors(&without_index, depth - 1) { + return true; + }; + } + return false; + } + } + true +} + +fn parse_input_file(filename: &str) -> Vec> { + let mut reports: Vec> = Vec::new(); + let file = fs::read_to_string(filename).expect("Should open the file"); + + for line in file.lines() { + let numbers: Vec = line + .split_whitespace() + .map(|num| num.parse::().unwrap()) + .collect::>(); + reports.push(numbers); + } + + reports +}