|
| 1 | +use std::collections::HashMap; |
| 2 | +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; |
| 3 | +use std::str::FromStr; |
| 4 | +use std::sync::OnceLock; |
| 5 | +use itertools::Itertools; |
| 6 | +use aoclp::anyhow::Context; |
| 7 | +use aoclp::regex::Regex; |
| 8 | +use aoclp::solvers_impl::input::safe_get_input_as_many; |
| 9 | + |
| 10 | +pub fn part_1() -> usize { |
| 11 | + input().iter().map(Machine::fewest_presses_for_lights).sum() |
| 12 | +} |
| 13 | + |
| 14 | +pub fn part_2() -> usize { |
| 15 | + input().par_iter().map(Machine::fewest_presses_for_joltage).sum() |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Debug, Clone)] |
| 19 | +struct Machine { |
| 20 | + target_lights: Vec<bool>, |
| 21 | + button_wirings: Vec<Vec<usize>>, |
| 22 | + joltage_reqs: Vec<usize>, |
| 23 | +} |
| 24 | + |
| 25 | +impl Machine { |
| 26 | + fn fewest_presses_for_lights(&self) -> usize { |
| 27 | + let mut presses = 0; |
| 28 | + let mut states = vec![vec![false; self.target_lights.len()]]; |
| 29 | + loop { |
| 30 | + if states.iter().contains(&self.target_lights) { |
| 31 | + break presses; |
| 32 | + } |
| 33 | + |
| 34 | + states = states |
| 35 | + .into_iter() |
| 36 | + .flat_map(|l| { |
| 37 | + self.button_wirings |
| 38 | + .iter() |
| 39 | + .map(move |wiring| { |
| 40 | + let mut next = l.clone(); |
| 41 | + for w in wiring { |
| 42 | + next[*w] = !next[*w] |
| 43 | + } |
| 44 | + next |
| 45 | + }) |
| 46 | + }) |
| 47 | + .unique() |
| 48 | + .collect(); |
| 49 | + presses += 1; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fn fewest_presses_for_joltage(&self) -> usize { |
| 54 | + let mut cache = HashMap::new(); |
| 55 | + self.fewest_presses_for_joltage_from(vec![0; self.joltage_reqs.len()], 0, usize::MAX, &mut cache) |
| 56 | + } |
| 57 | + |
| 58 | + fn fewest_presses_for_joltage_from(&self, cur: Vec<usize>, steps: usize, max_steps: usize, cache: &mut HashMap<Vec<usize>, usize>) -> usize { |
| 59 | + if *cur == self.joltage_reqs { |
| 60 | + return steps; |
| 61 | + } else if let Some(presses) = cache.get(&cur) { |
| 62 | + return *presses; |
| 63 | + } else if steps == max_steps || cur.iter().zip(self.joltage_reqs.iter()).any(|(cur, req)| *cur > *req) { |
| 64 | + return max_steps; |
| 65 | + } |
| 66 | + |
| 67 | + let presses = self.button_wirings |
| 68 | + .iter() |
| 69 | + .fold(max_steps, |max, wiring| { |
| 70 | + let mut next = cur.clone(); |
| 71 | + for w in wiring { |
| 72 | + next[*w] += 1; |
| 73 | + } |
| 74 | + self.fewest_presses_for_joltage_from(next, steps + 1, max, cache) |
| 75 | + }); |
| 76 | + cache.insert(cur, presses); |
| 77 | + presses |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl FromStr for Machine { |
| 82 | + type Err = aoclp::Error; |
| 83 | + |
| 84 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 85 | + static REGEX: OnceLock<Regex> = OnceLock::new(); |
| 86 | + let re = REGEX.get_or_init(|| { |
| 87 | + Regex::new(r"^\s*\[(?<lights>[.#]+)]\s+(?<buttons>(?:\((?:\d+,?)+\)\s+)+)\s*\{(?<joltage>(?:\d+,?)+)}\s*$").unwrap() |
| 88 | + }); |
| 89 | + |
| 90 | + let captures = re.captures(s).with_context(|| format!("invalid machine spec: {s}"))?; |
| 91 | + let target_lights = &captures["lights"]; |
| 92 | + let button_wirings = &captures["buttons"]; |
| 93 | + let joltage_reqs = &captures["joltage"]; |
| 94 | + |
| 95 | + let target_lights = target_lights.chars().map(|c| c == '#').collect(); |
| 96 | + let button_wirings = button_wirings.split_ascii_whitespace().map(|bw| { |
| 97 | + bw[1..bw.len() - 1].split(',').map(|l| l.parse::<usize>()).try_collect() |
| 98 | + }).try_collect()?; |
| 99 | + let joltage_reqs = joltage_reqs.split(',').map(|j| j.parse::<usize>()).try_collect()?; |
| 100 | + |
| 101 | + Ok(Self { target_lights, button_wirings, joltage_reqs }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +fn input() -> Vec<Machine> { |
| 106 | + safe_get_input_as_many(2025, 10) |
| 107 | +} |
0 commit comments