From 37df1e747094938dfd9eb459aef301d578ea8e6c Mon Sep 17 00:00:00 2001 From: Oschly Date: Tue, 30 Dec 2025 22:30:54 +0100 Subject: [PATCH] day 4 part 2 + fix warnings --- src/day1/mod.rs | 1 + src/day2/id_range.rs | 2 - src/day2/mod.rs | 2 + src/day3/mod.rs | 1 + src/day4/mod.rs | 101 +------------------------------------- src/day4/part1.rs | 100 +++++++++++++++++++++++++++++++++++++ src/day4/part2.rs | 98 ++++++++++++++++++++++++++++++++++++ src/day4/sample_input.txt | 2 +- src/main.rs | 2 +- 9 files changed, 206 insertions(+), 103 deletions(-) create mode 100644 src/day4/part1.rs create mode 100644 src/day4/part2.rs diff --git a/src/day1/mod.rs b/src/day1/mod.rs index c133abb..f3925f2 100644 --- a/src/day1/mod.rs +++ b/src/day1/mod.rs @@ -2,6 +2,7 @@ use std::fs; mod dial_position; +#[allow(dead_code)] pub fn solve_part1() { let file = fs::read_to_string("src/day1/input.txt").expect("You didn't provide input.txt."); diff --git a/src/day2/id_range.rs b/src/day2/id_range.rs index e4b1e33..316d281 100644 --- a/src/day2/id_range.rs +++ b/src/day2/id_range.rs @@ -1,5 +1,3 @@ -use std::string; - pub struct IDRange { start: u64, end: u64, diff --git a/src/day2/mod.rs b/src/day2/mod.rs index ab891db..46b4273 100644 --- a/src/day2/mod.rs +++ b/src/day2/mod.rs @@ -20,11 +20,13 @@ fn parse_range(string: &str) -> IDRange { IDRange::new(lhs, rhs) } +#[allow(dead_code)] pub fn solve_part1() { let solution = solve(Some(2)); println!("The solution for part 1 is {}", solution); } +#[allow(dead_code)] pub fn solve_part2() { let solution = solve(None); println!("The solution for part 2 is {}", solution); diff --git a/src/day3/mod.rs b/src/day3/mod.rs index a142f68..b649c92 100644 --- a/src/day3/mod.rs +++ b/src/day3/mod.rs @@ -48,6 +48,7 @@ impl BatteryBank { } } +#[allow(dead_code)] pub fn solve() { let file = fs::read_to_string("src/day3/input.txt").expect("You didn't provide input.txt."); diff --git a/src/day4/mod.rs b/src/day4/mod.rs index 4d9325f..faaf542 100644 --- a/src/day4/mod.rs +++ b/src/day4/mod.rs @@ -1,99 +1,2 @@ -use std::{fs, io::Cursor, ops::Index}; - -pub fn solve() { - let file = fs::read_to_string("src/day4/input.txt").expect("You didn't provide input.txt."); - - let mut lines = file.lines().into_iter(); - - let mut previous_line = Option::<&str>::None; - let mut opt_current_line = lines.next(); - let mut next_line = lines.next(); - - let roll_symbol = "@".chars().next().unwrap(); - let mut accessible_rolls_count = 0; - - while let Some(current_line) = opt_current_line { - for (idx, el) in current_line.char_indices() { - if el != roll_symbol { - continue; - } - let mut adjacent_rolls_count = 0; - - if idx > 0 { - let left = current_line[(idx - 1)..].chars().next().unwrap(); - if left == roll_symbol { - adjacent_rolls_count += 1; - } - } - - if idx < current_line.len() - 1 { - let right = current_line[(idx + 1)..].chars().next().unwrap(); - - if right == roll_symbol { - adjacent_rolls_count += 1; - } - } - - if let Some(previous_line) = previous_line.clone() { - { - let above = previous_line[idx..].chars().next().unwrap(); - - if above == roll_symbol { - adjacent_rolls_count += 1; - }; - } - - if idx > 0 { - let above_left = previous_line[(idx - 1)..].chars().next().unwrap(); - - if above_left == roll_symbol { - adjacent_rolls_count += 1; - } - } - - if idx < previous_line.len() - 1 { - let above_right = previous_line[(idx + 1)..].chars().next().unwrap(); - - if above_right == roll_symbol { - adjacent_rolls_count += 1; - } - } - } - - if let Some(next_line) = next_line { - { - let below = next_line[idx..].chars().next().unwrap(); - - if below == roll_symbol { - adjacent_rolls_count += 1; - } - } - - if idx > 0 { - let below_left = next_line[(idx - 1)..].chars().next().unwrap(); - - if below_left == roll_symbol { - adjacent_rolls_count += 1; - } - } - - if idx < next_line.len() - 1 { - let below_right = next_line[(idx + 1)..].chars().next().unwrap(); - - if below_right == roll_symbol { - adjacent_rolls_count += 1; - } - } - } - - if adjacent_rolls_count < 4 { - accessible_rolls_count += 1; - } - } - previous_line = Some(current_line); - opt_current_line = next_line; - next_line = lines.next(); - } - - println!("{}", accessible_rolls_count); -} +pub mod part1; +pub mod part2; diff --git a/src/day4/part1.rs b/src/day4/part1.rs new file mode 100644 index 0000000..a62f1f8 --- /dev/null +++ b/src/day4/part1.rs @@ -0,0 +1,100 @@ +use std::fs; + +#[allow(dead_code)] +pub fn solve() { + let file = fs::read_to_string("src/day4/input.txt").expect("You didn't provide input.txt."); + + let mut lines = file.lines().into_iter(); + + let mut previous_line = Option::<&str>::None; + let mut opt_current_line = lines.next(); + let mut next_line = lines.next(); + + let roll_symbol = "@".chars().next().unwrap(); + let mut accessible_rolls_count = 0; + + while let Some(current_line) = opt_current_line { + for (idx, el) in current_line.char_indices() { + if el != roll_symbol { + continue; + } + let mut adjacent_rolls_count = 0; + + if idx > 0 { + let left = current_line[(idx - 1)..].chars().next().unwrap(); + if left == roll_symbol { + adjacent_rolls_count += 1; + } + } + + if idx < current_line.len() - 1 { + let right = current_line[(idx + 1)..].chars().next().unwrap(); + + if right == roll_symbol { + adjacent_rolls_count += 1; + } + } + + if let Some(previous_line) = previous_line.clone() { + { + let above = previous_line[idx..].chars().next().unwrap(); + + if above == roll_symbol { + adjacent_rolls_count += 1; + }; + } + + if idx > 0 { + let above_left = previous_line[(idx - 1)..].chars().next().unwrap(); + + if above_left == roll_symbol { + adjacent_rolls_count += 1; + } + } + + if idx < previous_line.len() - 1 { + let above_right = previous_line[(idx + 1)..].chars().next().unwrap(); + + if above_right == roll_symbol { + adjacent_rolls_count += 1; + } + } + } + + if let Some(next_line) = next_line { + { + let below = next_line[idx..].chars().next().unwrap(); + + if below == roll_symbol { + adjacent_rolls_count += 1; + } + } + + if idx > 0 { + let below_left = next_line[(idx - 1)..].chars().next().unwrap(); + + if below_left == roll_symbol { + adjacent_rolls_count += 1; + } + } + + if idx < next_line.len() - 1 { + let below_right = next_line[(idx + 1)..].chars().next().unwrap(); + + if below_right == roll_symbol { + adjacent_rolls_count += 1; + } + } + } + + if adjacent_rolls_count < 4 { + accessible_rolls_count += 1; + } + } + previous_line = Some(current_line); + opt_current_line = next_line; + next_line = lines.next(); + } + + println!("{}", accessible_rolls_count); +} diff --git a/src/day4/part2.rs b/src/day4/part2.rs new file mode 100644 index 0000000..a98f7da --- /dev/null +++ b/src/day4/part2.rs @@ -0,0 +1,98 @@ +use std::fs; + +#[allow(dead_code)] +pub fn solve() { + let file = fs::read_to_string("src/day4/input.txt").expect("You didn't provide input.txt."); + + let mut lines: Vec> = file + .lines() + .map(|line| line.chars().map(|char| char == '@').collect::>()) + .collect(); + + let mut accessible_rolls_count = 0; + let mut last_accessible_rolls_found = true; + + while last_accessible_rolls_found { + last_accessible_rolls_found = false; + + let mut line_iteration = 0; + let lines_copy = lines.clone(); + let mut lines_iter = lines_copy.iter(); + + let mut previous_line = Option::>::None; + let mut opt_current_line = lines_iter.next().clone(); + let mut next_line = lines_iter.next(); + + while let Some(current_line) = &opt_current_line { + for (idx, el) in current_line.iter().enumerate() { + if !el { + continue; + } + + let mut adjacent_rolls_count = 0; + + if idx > 0 { + if current_line[idx - 1] { + adjacent_rolls_count += 1; + } + } + + if idx < current_line.len() - 1 { + if current_line[idx + 1] { + adjacent_rolls_count += 1; + } + } + + if let Some(previous_line) = &previous_line { + if previous_line[idx] { + adjacent_rolls_count += 1; + }; + + if idx > 0 { + if previous_line[idx - 1] { + adjacent_rolls_count += 1; + } + } + + if idx < previous_line.len() - 1 { + if previous_line[idx + 1] { + adjacent_rolls_count += 1; + } + } + } + + if let Some(next_line) = &next_line { + { + if next_line[idx] { + adjacent_rolls_count += 1; + } + } + + if idx > 0 { + if next_line[idx - 1] { + adjacent_rolls_count += 1; + } + } + + if idx < next_line.len() - 1 { + if next_line[idx + 1] { + adjacent_rolls_count += 1; + } + } + } + + if adjacent_rolls_count < 4 { + accessible_rolls_count += 1; + lines[line_iteration][idx] = false; + last_accessible_rolls_found = true; + } + } + previous_line = Some(current_line.to_vec()); + opt_current_line = next_line; + next_line = lines_iter.next(); + line_iteration += 1; + } + } + + println!("{}", accessible_rolls_count); +} diff --git a/src/day4/sample_input.txt b/src/day4/sample_input.txt index 8209399..9ad769d 100644 --- a/src/day4/sample_input.txt +++ b/src/day4/sample_input.txt @@ -7,4 +7,4 @@ .@.@.@.@@@ @.@@@.@@@@ .@@@@@@@@. -@.@.@@@.@. +@.@.@@@.@. \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 529e899..9572f2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,5 +4,5 @@ mod day3; mod day4; fn main() { - day4::solve(); + day4::part2::solve(); }