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