day 4 part 2 + fix warnings

This commit is contained in:
2025-12-30 22:30:54 +01:00
parent 61a1686010
commit 37df1e7470
9 changed files with 206 additions and 103 deletions

View File

@@ -2,6 +2,7 @@ use std::fs;
mod dial_position; mod dial_position;
#[allow(dead_code)]
pub fn solve_part1() { pub fn solve_part1() {
let file = fs::read_to_string("src/day1/input.txt").expect("You didn't provide input.txt."); let file = fs::read_to_string("src/day1/input.txt").expect("You didn't provide input.txt.");

View File

@@ -1,5 +1,3 @@
use std::string;
pub struct IDRange { pub struct IDRange {
start: u64, start: u64,
end: u64, end: u64,

View File

@@ -20,11 +20,13 @@ fn parse_range(string: &str) -> IDRange {
IDRange::new(lhs, rhs) IDRange::new(lhs, rhs)
} }
#[allow(dead_code)]
pub fn solve_part1() { pub fn solve_part1() {
let solution = solve(Some(2)); let solution = solve(Some(2));
println!("The solution for part 1 is {}", solution); println!("The solution for part 1 is {}", solution);
} }
#[allow(dead_code)]
pub fn solve_part2() { pub fn solve_part2() {
let solution = solve(None); let solution = solve(None);
println!("The solution for part 2 is {}", solution); println!("The solution for part 2 is {}", solution);

View File

@@ -48,6 +48,7 @@ impl BatteryBank {
} }
} }
#[allow(dead_code)]
pub fn solve() { pub fn solve() {
let file = fs::read_to_string("src/day3/input.txt").expect("You didn't provide input.txt."); let file = fs::read_to_string("src/day3/input.txt").expect("You didn't provide input.txt.");

View File

@@ -1,99 +1,2 @@
use std::{fs, io::Cursor, ops::Index}; pub mod part1;
pub mod part2;
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);
}

100
src/day4/part1.rs Normal file
View File

@@ -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);
}

98
src/day4/part2.rs Normal file
View File

@@ -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<Vec<bool>> = file
.lines()
.map(|line| line.chars().map(|char| char == '@').collect::<Vec<bool>>())
.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::<Vec<bool>>::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);
}

View File

@@ -4,5 +4,5 @@ mod day3;
mod day4; mod day4;
fn main() { fn main() {
day4::solve(); day4::part2::solve();
} }