|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use std::collections::HashMap; |
| 3 | + |
| 4 | +fn connections_from_line(inp: &str) -> Result<Vec<(&str, &str)>> { |
| 5 | + let (here, there) = inp.split_once(": ").context("bad format")?; |
| 6 | + |
| 7 | + Ok(there |
| 8 | + .split(" ") |
| 9 | + .flat_map(|x| [(here, x)]) |
| 10 | + .collect::<Vec<_>>()) |
| 11 | +} |
| 12 | + |
| 13 | +fn pathfind(connections: &[(&str, &str)], start: &str, end: &str) -> u64 { |
| 14 | + let mut map = HashMap::<&str, u64>::default(); |
| 15 | + map.insert(start, 1); |
| 16 | + |
| 17 | + let mut ans = 0; |
| 18 | + |
| 19 | + for _ in 0..connections.len() { |
| 20 | + let mut new = HashMap::<&str, u64>::default(); |
| 21 | + |
| 22 | + connections.iter().for_each(|(s, e)| { |
| 23 | + if let Some(oldval) = map.get(s) { |
| 24 | + *new.entry(e).or_insert(0) += oldval; |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + ans += *new.get(end).unwrap_or(&0); |
| 29 | + |
| 30 | + std::mem::swap(&mut new, &mut map); |
| 31 | + } |
| 32 | + |
| 33 | + ans |
| 34 | +} |
| 35 | + |
| 36 | +fn parse(inp: &str) -> Result<Vec<(&str, &str)>> { |
| 37 | + let connections = inp |
| 38 | + .trim() |
| 39 | + .lines() |
| 40 | + .map(connections_from_line) |
| 41 | + .collect::<Result<Vec<_>>>()?; |
| 42 | + |
| 43 | + Ok(connections.into_iter().flatten().collect::<Vec<_>>()) |
| 44 | +} |
| 45 | + |
| 46 | +fn calculate_p1(connections: &[(&str, &str)]) -> u64 { |
| 47 | + pathfind(connections, "you", "out") |
| 48 | +} |
| 49 | + |
| 50 | +fn calculate_p2(connections: &[(&str, &str)]) -> u64 { |
| 51 | + (pathfind(connections, "svr", "dac") |
| 52 | + * pathfind(connections, "dac", "fft") |
| 53 | + * pathfind(connections, "fft", "out")) |
| 54 | + + (pathfind(connections, "svr", "fft") |
| 55 | + * pathfind(connections, "fft", "dac") |
| 56 | + * pathfind(connections, "dac", "out")) |
| 57 | +} |
| 58 | + |
| 59 | +pub fn run_2025_11(inp: &str) -> Result<String> { |
| 60 | + let connections = parse(inp)?; |
| 61 | + let p1 = calculate_p1(&connections); |
| 62 | + let p2 = calculate_p2(&connections); |
| 63 | + Ok(format!("{p1}\n{p2}")) |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(test)] |
| 67 | +mod tests { |
| 68 | + use super::*; |
| 69 | + |
| 70 | + const EXAMPLE_DATA: &str = include_str!("../inputs/examples/2025_11"); |
| 71 | + const EXAMPLE_DATA_P2: &str = include_str!("../inputs/examples/2025_11_2"); |
| 72 | + const REAL_DATA: &str = include_str!("../inputs/real/2025_11"); |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn test_example_p1() { |
| 76 | + assert_eq!(calculate_p1(&parse(EXAMPLE_DATA).unwrap()), 5); |
| 77 | + } |
| 78 | + |
| 79 | + #[test] |
| 80 | + fn test_example_p2() { |
| 81 | + assert_eq!(calculate_p2(&parse(EXAMPLE_DATA_P2).unwrap()), 2); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_real_p1() { |
| 86 | + assert_eq!(calculate_p1(&parse(REAL_DATA).unwrap()), 428); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_real_p2() { |
| 91 | + assert_eq!(calculate_p2(&parse(REAL_DATA).unwrap()), 331468292364745); |
| 92 | + } |
| 93 | +} |
0 commit comments