|
| 1 | +// Copyright 2024 Jedrzej Stuczynski |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use anyhow::Context; |
| 16 | +use aoc_common::helpers::digits_to_number; |
| 17 | +use std::str::FromStr; |
| 18 | + |
| 19 | +#[derive(Debug, Clone)] |
| 20 | +pub struct BatteryBank { |
| 21 | + pub batteries: Vec<usize>, |
| 22 | +} |
| 23 | + |
| 24 | +impl BatteryBank { |
| 25 | + fn maximum_joltage(&self, num_digits: usize) -> usize { |
| 26 | + let mut digits = Vec::new(); |
| 27 | + let mut remaining_to_choose = num_digits; |
| 28 | + |
| 29 | + let len = self.batteries.len(); |
| 30 | + if len < num_digits { |
| 31 | + return 0; |
| 32 | + } |
| 33 | + if len == num_digits { |
| 34 | + return digits_to_number(&self.batteries); |
| 35 | + } |
| 36 | + |
| 37 | + let mut window_start = 0; |
| 38 | + for _ in 0..num_digits { |
| 39 | + let mut chosen = 0; |
| 40 | + |
| 41 | + // our unused window must always be sufficiently big for the remaining digits |
| 42 | + let selection_window = &self.batteries[window_start..len - remaining_to_choose + 1]; |
| 43 | + |
| 44 | + // check if we simply run out of choices |
| 45 | + if self.batteries[window_start..].len() == remaining_to_choose { |
| 46 | + digits.extend_from_slice(&self.batteries[window_start..]); |
| 47 | + break; |
| 48 | + } |
| 49 | + |
| 50 | + let mut found_at_index = 0; |
| 51 | + for (i, value) in selection_window.iter().enumerate() { |
| 52 | + if *value > chosen { |
| 53 | + chosen = *value; |
| 54 | + found_at_index = i; |
| 55 | + } |
| 56 | + if *value == 9 { |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + window_start += found_at_index + 1; |
| 61 | + remaining_to_choose -= 1; |
| 62 | + |
| 63 | + digits.push(chosen); |
| 64 | + } |
| 65 | + |
| 66 | + digits_to_number(&digits) |
| 67 | + } |
| 68 | + |
| 69 | + pub fn maximum_joltage_with_two(&self) -> usize { |
| 70 | + self.maximum_joltage(2) |
| 71 | + } |
| 72 | + |
| 73 | + pub fn maximum_joltage_with_twelve(&self) -> usize { |
| 74 | + self.maximum_joltage(12) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl FromStr for BatteryBank { |
| 79 | + type Err = anyhow::Error; |
| 80 | + |
| 81 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 82 | + let batteries = s |
| 83 | + .chars() |
| 84 | + .map(|c| { |
| 85 | + c.to_digit(10) |
| 86 | + .map(|d| d as usize) |
| 87 | + .context("invalid battery value") |
| 88 | + }) |
| 89 | + .collect::<Result<Vec<_>, _>>()?; |
| 90 | + Ok(BatteryBank { batteries }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use super::*; |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn maximum_joltage_with_two() { |
| 100 | + let bank = BatteryBank::from_str("987654321111111").unwrap(); |
| 101 | + assert_eq!(bank.maximum_joltage_with_two(), 98); |
| 102 | + |
| 103 | + let bank = BatteryBank::from_str("811111111111119").unwrap(); |
| 104 | + assert_eq!(bank.maximum_joltage_with_two(), 89); |
| 105 | + |
| 106 | + let bank = BatteryBank::from_str("234234234234278").unwrap(); |
| 107 | + assert_eq!(bank.maximum_joltage_with_two(), 78); |
| 108 | + |
| 109 | + let bank = BatteryBank::from_str("818181911112111").unwrap(); |
| 110 | + assert_eq!(bank.maximum_joltage_with_two(), 92); |
| 111 | + } |
| 112 | + |
| 113 | + #[test] |
| 114 | + fn maximum_joltage_with_twelve() { |
| 115 | + let bank = BatteryBank::from_str("987654321111111").unwrap(); |
| 116 | + assert_eq!(bank.maximum_joltage_with_twelve(), 987654321111); |
| 117 | + |
| 118 | + let bank = BatteryBank::from_str("811111111111119").unwrap(); |
| 119 | + assert_eq!(bank.maximum_joltage_with_twelve(), 811111111119); |
| 120 | + |
| 121 | + let bank = BatteryBank::from_str("234234234234278").unwrap(); |
| 122 | + assert_eq!(bank.maximum_joltage_with_twelve(), 434234234278); |
| 123 | + |
| 124 | + let bank = BatteryBank::from_str("818181911112111").unwrap(); |
| 125 | + assert_eq!(bank.maximum_joltage_with_twelve(), 888911112111); |
| 126 | + } |
| 127 | +} |
0 commit comments