2025: day03: part 2

This commit is contained in:
Antoine Martin 2025-12-08 12:26:58 +01:00
parent 8a22da8ba1
commit 261480afa3

View file

@ -7,14 +7,12 @@ const INPUT: &str = include_str!("../input/day03.txt");
pub fn run() -> Result<String> { pub fn run() -> Result<String> {
let mut res = String::with_capacity(128); let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?; writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res) Ok(res)
} }
fn find_max_tens_digit(bank: &[u8]) -> Option<(usize, u8)> { fn find_max_digit(bank: &[u8]) -> Option<(usize, u8)> {
// We don't include the last byte since it cannot be the digit for tens, otherwise we'd have no bank.iter()
// byte left for the units digit.
bank[..bank.len() - 1]
.iter()
.copied() .copied()
.enumerate() .enumerate()
// We want the max, but use min + Reverse because min selects the FIRST minimum, while max // We want the max, but use min + Reverse because min selects the FIRST minimum, while max
@ -23,22 +21,37 @@ fn find_max_tens_digit(bank: &[u8]) -> Option<(usize, u8)> {
.min_by_key(|&(_idx, val)| cmp::Reverse(val)) .min_by_key(|&(_idx, val)| cmp::Reverse(val))
} }
fn find_max_units_digit(sub_bank: &[u8]) -> Option<u8> { fn compute_max_joltage(mut bank: &[u8], batteries: usize) -> Result<u64> {
sub_bank.iter().copied().max() let mut joltage = 0;
} for battery in 0..batteries {
// We don't include the last [batteries - battery - 1] bytes since we need at least these other
// digits for the final number
let not_in_search = batteries - battery - 1;
let (idx, digit) = find_max_digit(&bank[..(bank.len() - not_in_search)])
.context("couldn't find max digit in bank")?;
fn compute_max_joltage(bank: &[u8]) -> Result<u64> { let digit = (digit - b'0') as u64;
let (idx, tens) = find_max_tens_digit(bank).context("couldn't find tens digit in bank")?; joltage = joltage * 10 + digit;
let units = bank = &bank[(idx + 1)..];
find_max_units_digit(&bank[(idx + 1)..]).context("couldn't find units digit in bank")?; }
let (tens, units) = ((tens - b'0') as u64, (units - b'0') as u64); Ok(joltage)
Ok(tens * 10 + units)
} }
fn part1(input: &str) -> Result<u64> { fn part1(input: &str) -> Result<u64> {
let mut sum = 0; let mut sum = 0;
for line in input.lines() { for line in input.lines() {
let joltage = compute_max_joltage(line.as_bytes()) let joltage = compute_max_joltage(line.as_bytes(), 2)
.with_context(|| format!("couldn't compute joltage for bank `{}'", line))?;
sum += joltage;
}
Ok(sum)
}
fn part2(input: &str) -> Result<u64> {
let mut sum = 0;
for line in input.lines() {
let joltage = compute_max_joltage(line.as_bytes(), 12)
.with_context(|| format!("couldn't compute joltage for bank `{}'", line))?; .with_context(|| format!("couldn't compute joltage for bank `{}'", line))?;
sum += joltage; sum += joltage;
} }
@ -55,23 +68,23 @@ mod tests {
#[test] #[test]
fn part1_provided() { fn part1_provided() {
assert_eq!( assert_eq!(
compute_max_joltage("987654321111111".as_bytes()).ok(), compute_max_joltage("987654321111111".as_bytes(), 2).ok(),
Some(98) Some(98)
); );
assert_eq!( assert_eq!(
compute_max_joltage("811111111111119".as_bytes()).ok(), compute_max_joltage("811111111111119".as_bytes(), 2).ok(),
Some(89) Some(89)
); );
assert_eq!( assert_eq!(
compute_max_joltage("234234234234278".as_bytes()).ok(), compute_max_joltage("234234234234278".as_bytes(), 2).ok(),
Some(78) Some(78)
); );
assert_eq!( assert_eq!(
compute_max_joltage("818181911112111".as_bytes()).ok(), compute_max_joltage("818181911112111".as_bytes(), 2).ok(),
Some(92) Some(92)
); );
// added this test to check for min + Reverse usage // added this test to check for min + Reverse usage
assert_eq!(compute_max_joltage("9892".as_bytes()).ok(), Some(99)); assert_eq!(compute_max_joltage("9892".as_bytes(), 2).ok(), Some(99));
assert_eq!(part1(PROVIDED).unwrap(), 357); assert_eq!(part1(PROVIDED).unwrap(), 357);
} }
@ -80,13 +93,13 @@ mod tests {
assert_eq!(part1(INPUT).unwrap(), 17034); assert_eq!(part1(INPUT).unwrap(), 17034);
} }
//#[test] #[test]
//fn part2_provided() { fn part2_provided() {
// assert_eq!(part2(PROVIDED).unwrap(), 4174379265); assert_eq!(part2(PROVIDED).unwrap(), 3121910778619);
//} }
//#[test] #[test]
//fn part2_real() { fn part2_real() {
// assert_eq!(part2(INPUT).unwrap(), 31680313976); assert_eq!(part2(INPUT).unwrap(), 168798209663590);
//} }
} }