diff --git a/2021/input/2021/day12.txt b/2021/input/2021/day12.txt new file mode 100644 index 0000000..123aa72 --- /dev/null +++ b/2021/input/2021/day12.txt @@ -0,0 +1,22 @@ +zs-WO +zs-QJ +WO-zt +zs-DP +WO-end +gv-zt +iu-SK +HW-zs +iu-WO +gv-WO +gv-start +gv-DP +start-WO +HW-zt +iu-HW +gv-HW +zs-SK +HW-end +zs-end +DP-by +DP-iu +zt-start diff --git a/2021/src/day12.rs b/2021/src/day12.rs new file mode 100644 index 0000000..d6b8491 --- /dev/null +++ b/2021/src/day12.rs @@ -0,0 +1,96 @@ +use std::{ + collections::HashMap, + fmt::{Debug, Error, Formatter}, + num::ParseIntError, + ops::{Index, IndexMut}, + str::FromStr, +}; + +use anyhow::Result; +use aoc_runner_derive::{aoc, aoc_generator}; +use thiserror::Error; + +struct Node { + name: String, + small: bool, + neighbors: Vec, +} + +struct Graph { + nodes: Vec, +} + +fn search(node: &str, nodes: &HashMap<&str, Vec<&str>>, path: String, paths: &mut Vec) { + if node == "end" { + paths.push(path); + return; + } + for neighbor in &nodes[node] { + // If lowercase. + if neighbor.as_bytes()[0] & 0x20 != 0 { + if path.contains(neighbor) { + continue; + } + } + search(neighbor, nodes, format!("{},{}", path, neighbor), paths); + } +} + +fn paths(nodes: &HashMap<&str, Vec<&str>>) -> usize { + let mut paths = Vec::new(); + search("start", nodes, "start".to_string(), &mut paths); + //dbg!(&paths); + paths.len() +} + +#[aoc(day12, part1)] +fn part1(input: &str) -> Result { + let mut nodes = HashMap::new(); + input.lines().for_each(|p| { + let (n1, n2) = p.split_once('-').expect("missing dash"); + nodes.entry(n1).or_insert(Vec::new()).push(n2); + nodes.entry(n2).or_insert(Vec::new()).push(n1); + }); + Ok(paths(&nodes)) +} + +/* +#[aoc(day12, part2)] +fn part2(input: &[u64]) -> Result { +todo!("part2"); +Ok(0) +} +*/ + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part1() -> Result<()> { + let input = r#" +start-A +start-b +A-c +A-b +b-d +A-end +b-end +"# + .trim(); + + assert_eq!(part1(input)?, 10); + Ok(()) + } + + /* + #[test] + fn test_part2()->Result<()> { + let input = r#" + "# + .trim(); + assert_eq!(part2(&parse(input)?)?, u64::MAX); + Ok(()) + } + */ +} diff --git a/2021/src/lib.rs b/2021/src/lib.rs index a9465b2..b37d517 100644 --- a/2021/src/lib.rs +++ b/2021/src/lib.rs @@ -1,6 +1,7 @@ pub mod day1; pub mod day10; pub mod day11; +pub mod day12; pub mod day2; pub mod day3; pub mod day4;