Add solution for "Day 3: Lobby", part 2
This commit is contained in:
@@ -24,6 +24,12 @@ Firstly, we calculate directly the sum of invalid IDs composed of two identical
|
|||||||
|
|
||||||
On top of that we use a cache to avoid recalculating prime factors and certain helper variables for given ID lengths and repetition counts.
|
On top of that we use a cache to avoid recalculating prime factors and certain helper variables for given ID lengths and repetition counts.
|
||||||
|
|
||||||
|
### Day 3: Lobby
|
||||||
|
|
||||||
|
:mag_right: Puzzle: <https://adventofcode.com/2025/day/3>, :white_check_mark: Solver: [`Lobby`](src/solvers/lobby.rs)
|
||||||
|
|
||||||
|
With a continuously updated tally of the current best joltage initialized at zero, we go once through each digit string from left to right and try to maximize the digit with the highest posiible significance in our joltage as we go, as long as the less significant digits in the joltage are not more than what remains in the string. If a better digit was encountered, all less significant digits are reset to zero.
|
||||||
|
|
||||||
### Day 4: Printing Department
|
### Day 4: Printing Department
|
||||||
|
|
||||||
:mag_right: Puzzle: <https://adventofcode.com/2025/day/4>, :white_check_mark: Solver: [`PrintingDepartment`](src/solvers/printing_department.rs)
|
:mag_right: Puzzle: <https://adventofcode.com/2025/day/4>, :white_check_mark: Solver: [`PrintingDepartment`](src/solvers/printing_department.rs)
|
||||||
|
|||||||
+19
-14
@@ -4,20 +4,24 @@ use std::io::BufRead;
|
|||||||
pub struct Lobby {}
|
pub struct Lobby {}
|
||||||
|
|
||||||
impl Lobby {
|
impl Lobby {
|
||||||
fn largest_joltage(bank: &str) -> u64 {
|
const PART1_MAX_BATTERIES: usize = 2;
|
||||||
let mut best = (0, 0);
|
const PART2_MAX_BATTERIES: usize = 12;
|
||||||
for d in bank.bytes().take(bank.len() - 1) {
|
|
||||||
if best.0 < d {
|
fn largest_joltage(bank: &str, max_batteries: usize) -> u64 {
|
||||||
best = (d, 0)
|
let mut best = vec![0_u8; max_batteries];
|
||||||
} else if best.1 < d {
|
for (i, d) in bank.bytes().map(|c| c - '0' as u8).enumerate() {
|
||||||
best.1 = d;
|
let mut fill_index = max_batteries;
|
||||||
|
let skip = (max_batteries + i).saturating_sub(bank.len());
|
||||||
|
for (j, b) in best.iter_mut().skip(skip).enumerate() {
|
||||||
|
if *b < d {
|
||||||
|
*b = d;
|
||||||
|
fill_index = j + 1 + skip;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let last = bank.bytes().last().unwrap();
|
best[fill_index..].fill(0);
|
||||||
if best.1 < last {
|
|
||||||
best.1 = last;
|
|
||||||
}
|
}
|
||||||
u64::from((best.0 - '0' as u8) * 10 + (best.1 - '0' as u8))
|
u64::from(best.iter().fold(0, |acc: u64, &x| acc * 10 + u64::from(x)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,10 +30,11 @@ impl Solver for Lobby {
|
|||||||
const PUZZLE_NAME: &'static str = "Lobby";
|
const PUZZLE_NAME: &'static str = "Lobby";
|
||||||
|
|
||||||
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) {
|
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) {
|
||||||
let mut joltage = 0;
|
let mut joltages = (0, 0);
|
||||||
for line in reader.lines().map_while(Result::ok) {
|
for line in reader.lines().map_while(Result::ok) {
|
||||||
joltage += Self::largest_joltage(&line);
|
joltages.0 += Self::largest_joltage(&line, Self::PART1_MAX_BATTERIES);
|
||||||
|
joltages.1 += Self::largest_joltage(&line, Self::PART2_MAX_BATTERIES);
|
||||||
}
|
}
|
||||||
(joltage, 0)
|
joltages
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -27,7 +27,7 @@ fn gift_shop() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn lobby() {
|
fn lobby() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Ok((357, 0)),
|
Ok((357, 3121910778619)),
|
||||||
solvers::run_solver(solvers::lobby::Lobby {}, &EXAMPLE_PATHS)
|
solvers::run_solver(solvers::lobby::Lobby {}, &EXAMPLE_PATHS)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -22,7 +22,7 @@ fn gift_shop() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn lobby() {
|
fn lobby() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Ok((17144, 0)),
|
Ok((17144, 170371185255900)),
|
||||||
solvers::run_solver(solvers::lobby::Lobby {}, &solvers::DATA_PATHS)
|
solvers::run_solver(solvers::lobby::Lobby {}, &solvers::DATA_PATHS)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user