my solutions to advent of code
aoc advent-of-code
1use std::collections::{HashMap, HashSet}; 2 3fn main() { 4 let input = include_str!("../../input.txt"); 5 let input: Vec<&str> = input.trim().split("\n").collect(); 6 7 let combinations: HashMap<String, Vec<String>> = { 8 let mut combinations = HashMap::new(); 9 input[..input.len() - 2].iter().for_each(|v| { 10 let v: Vec<&str> = v.split(" ").collect(); 11 match v[..] { 12 [original, "=>", target] => { 13 let entry = combinations 14 .entry(original.to_string()) 15 .or_insert(Vec::with_capacity(1)); 16 entry.push(target.to_string()); 17 } 18 _ => panic!("{:?}", v), 19 } 20 }); 21 combinations 22 }; 23 24 let medicine_mol = input.last().unwrap(); 25 26 // part 1 27 { 28 let mol_len = medicine_mol.len(); 29 let mut possibilities: HashSet<String> = HashSet::new(); 30 for i in 0..mol_len { 31 let (a, b) = medicine_mol.split_at(i); 32 combinations.iter().for_each(|(from, to)| { 33 to.iter().for_each(|to| { 34 possibilities.insert(format!( 35 "{}{}", 36 a, 37 b.replacen(from, to, 1) 38 )); 39 }) 40 }); 41 } 42 println!("Part 1: {}", possibilities.len() - 1) 43 } 44}