feat: Day 3 is completado
Some checks failed
Continuous integration / Check (push) Successful in 43s
Continuous integration / Test Suite (push) Successful in 46s
Continuous integration / Rustfmt (push) Successful in 31s
Continuous integration / Clippy (push) Failing after 43s
Continuous integration / build (push) Successful in 47s

This commit is contained in:
2025-12-03 20:04:55 +00:00
parent 0dee050ef2
commit a8a7f91132
5 changed files with 296 additions and 9 deletions

View File

@@ -14,7 +14,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let days: Vec<Box<dyn Solution>> = vec![
Box::new(day01::Day01 {}),
Box::new(day02::Day02 {}),
// Box::new(day03::Day03 {}),
Box::new(day03::Day03 {}),
// Box::new(day04::Day04 {}),
// Box::new(day05::Day05 {}),
// Box::new(day06::Day06 {}),
@@ -64,7 +64,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
println!(
"Ran AoC 2023 in {}ms",
"Ran AoC 2025 in {}ms",
SystemTime::now().duration_since(start_time)?.as_millis()
);
Ok(())

View File

@@ -5,16 +5,32 @@ pub struct Day03 {}
impl Solution for Day03 {
fn part1(
&self,
_input: &mut Vec<String>,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready"))
let battery_banks: Vec<Vec<u8>> = self.collect_banks(input);
let batteries = 2;
let mut total: u64 = 0;
for bank in battery_banks {
total += self.calculate_joltage(batteries, &bank);
}
Ok(Box::new(total))
}
fn part2(
&self,
_input: &mut Vec<String>,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready"))
let battery_banks: Vec<Vec<u8>> = self.collect_banks(input);
let batteries = 12;
let mut total: u64 = 0;
for bank in battery_banks {
total += self.calculate_joltage(batteries, &bank);
}
Ok(Box::new(total))
}
fn get_day(&self) -> u8 {
@@ -22,10 +38,73 @@ impl Solution for Day03 {
}
}
impl Day03 {}
impl Day03 {
/// Collect battery banks from input
///
/// # Arguments
/// * `arr` - Vec of strings representing battery banks
///
/// # Returns
/// * `Vec<Vec<u8>>` - Vector of battery banks as vectors of u8s
fn collect_banks(&self, arr: &Vec<String>) -> Vec<Vec<u8>> {
let mut battery_banks: Vec<Vec<u8>> = vec![];
// Maps Battery banks into vec of u8s
for bank in arr {
let n = bank
.as_str()
.chars()
.filter_map(|c| c.to_digit(10))
.map(|d| d as u8)
.collect();
battery_banks.push(n);
}
battery_banks
}
/// Scan a slice of u8s to find the max int,
/// (including its location) within the slice
fn get_max(&self, arr: &[u8]) -> (u8, usize) {
let mut max: u8 = 0;
let mut max_idx: usize = 0;
for (idx, i) in arr.iter().enumerate() {
if *i > max {
max = *i;
max_idx = idx;
}
}
(max, max_idx)
}
/// Calculate the joltage given a number of batteries and a bank
///
/// # Arguments
/// * `num_batteries` - Number of batteries to consider
/// * `bank` - Slice of u8s representing the battery bank
///
/// # Returns
/// * `u64` - The calculated joltage
fn calculate_joltage(&self, num_batteries: u8, bank: &[u8]) -> u64 {
// Find the highest val in the avilable bank slice
// leaving enough room for the remaining batteries
let (max_val, max_idx) = self.get_max(&bank[0..=(bank.len() - (num_batteries as usize))]);
// Base case: only one battery left to place
if num_batteries == 1 {
return max_val as u64;
}
// Otherwise, place the battery and recurse
return (max_val as u64) * 10u64.pow((num_batteries - 1) as u32)
+ self.calculate_joltage(num_batteries - 1, &bank[max_idx + 1..bank.len()]);
}
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
use crate::*;
@@ -44,7 +123,7 @@ mod test {
.unwrap()
.to_string();
assert_eq!(answer, "Ready");
assert_eq!(answer, "357");
}
#[test]
@@ -61,6 +140,6 @@ mod test {
.unwrap()
.to_string();
assert_eq!(answer, "Ready");
assert_eq!(answer, "3121910778619");
}
}