|
| 1 | +use ::day_14::{parse_input, Item}; |
| 2 | +use std::collections::{HashMap, LinkedList}; |
| 3 | + |
| 4 | +#[derive(Clone, Eq, PartialEq, Hash, Debug)] |
| 5 | +struct Output { |
| 6 | + pub quantity: u8, |
| 7 | + pub required: Vec<Item>, |
| 8 | +} |
| 9 | + |
| 10 | +fn to_map(input: &Vec<(Item, Vec<Item>)>) -> HashMap<String, Output> { |
| 11 | + let mut map = HashMap::new(); |
| 12 | + |
| 13 | + for (key, value) in input { |
| 14 | + map.insert( |
| 15 | + key.0.to_owned(), |
| 16 | + Output { |
| 17 | + quantity: key.1, |
| 18 | + required: value.to_owned(), |
| 19 | + }, |
| 20 | + ); |
| 21 | + } |
| 22 | + |
| 23 | + map |
| 24 | +} |
| 25 | + |
| 26 | +fn get_requirements(map: &HashMap<String, Output>) -> Vec<Vec<String>> { |
| 27 | + let fuel = &map.get("FUEL").unwrap(); |
| 28 | + let mut paths = vec![]; |
| 29 | + for item in &fuel.required { |
| 30 | + let mut req = LinkedList::new(); |
| 31 | + req.push_front((item.clone(), vec![item.0.to_owned()])); |
| 32 | + while req.len() > 0 { |
| 33 | + if let Some(((key, _value), tree)) = req.pop_back() { |
| 34 | + match map.get(&key) { |
| 35 | + Some(item) => { |
| 36 | + for required in &item.required { |
| 37 | + let mut tree = tree.clone(); |
| 38 | + if !tree.contains(&key) { |
| 39 | + tree.insert(0, key.to_owned()); |
| 40 | + } |
| 41 | + if !tree.contains(&required.0) { |
| 42 | + tree.insert(0, required.0.to_owned()); |
| 43 | + } |
| 44 | + req.push_back((required.clone(), tree.to_owned())); |
| 45 | + } |
| 46 | + } |
| 47 | + None => paths.push(tree.clone()), |
| 48 | + }; |
| 49 | + }; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + paths |
| 54 | +} |
| 55 | + |
| 56 | +fn part_01(input: &Vec<(Item, Vec<Item>)>) -> u32 { |
| 57 | + let map = to_map(input); |
| 58 | + let requirements = get_requirements(&map); |
| 59 | + |
| 60 | + for req in requirements { |
| 61 | + println!("{:?}", req); |
| 62 | + } |
| 63 | + 0 |
| 64 | +} |
| 65 | + |
| 66 | +fn main() { |
| 67 | + let input = parse_input(include_str!("../../input/day_14")); |
| 68 | + println!("part_01: {}", part_01(&input)); |
| 69 | +} |
| 70 | + |
| 71 | +#[cfg(test)] |
| 72 | +#[allow(dead_code, unused_imports)] |
| 73 | +mod tests { |
| 74 | + use super::*; |
| 75 | + |
| 76 | + const EXAMPLE_01: &'static str = " |
| 77 | + 9 ORE => 2 A |
| 78 | + 8 ORE => 3 B |
| 79 | + 7 ORE => 5 C |
| 80 | + 3 A, 4 B => 1 AB |
| 81 | + 5 B, 7 C => 1 BC |
| 82 | + 4 C, 1 A => 1 CA |
| 83 | + 2 AB, 3 BC, 4 CA => 1 FUEL |
| 84 | + "; |
| 85 | + |
| 86 | + const EXAMPLE_02: &'static str = " |
| 87 | + 157 ORE => 5 NZVS |
| 88 | + 165 ORE => 6 DCFZ |
| 89 | + 44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL |
| 90 | + 12 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ |
| 91 | + 179 ORE => 7 PSHF |
| 92 | + 177 ORE => 5 HKGWZ |
| 93 | + 7 DCFZ, 7 PSHF => 2 XJWVT |
| 94 | + 165 ORE => 2 GPVTF |
| 95 | + 3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT |
| 96 | + "; |
| 97 | + |
| 98 | + const EXAMPLE_03: &'static str = " |
| 99 | + 2 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG |
| 100 | + 17 NVRVD, 3 JNWZP => 8 VPVL |
| 101 | + 53 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL |
| 102 | + 22 VJHF, 37 MNCFX => 5 FWMGM |
| 103 | + 139 ORE => 4 NVRVD |
| 104 | + 144 ORE => 7 JNWZP |
| 105 | + 5 MNCFX, 7 RFSQX, 2 FWMGM, 2 VPVL, 19 CXFTF => 3 HVMC |
| 106 | + 5 VJHF, 7 MNCFX, 9 VPVL, 37 CXFTF => 6 GNMV |
| 107 | + 145 ORE => 6 MNCFX |
| 108 | + 1 NVRVD => 8 CXFTF |
| 109 | + 1 VJHF, 6 MNCFX => 4 RFSQX |
| 110 | + 176 ORE => 6 VJHF |
| 111 | + "; |
| 112 | + |
| 113 | + const EXAMPLE_04: &'static str = " |
| 114 | + 171 ORE => 8 CNZTR |
| 115 | + 7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL |
| 116 | + 114 ORE => 4 BHXH |
| 117 | + 14 VRPVC => 6 BMBT |
| 118 | + 6 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL |
| 119 | + 6 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT |
| 120 | + 15 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW |
| 121 | + 13 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW |
| 122 | + 5 BMBT => 4 WPTQ |
| 123 | + 189 ORE => 9 KTJDG |
| 124 | + 1 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP |
| 125 | + 12 VRPVC, 27 CNZTR => 2 XDBXC |
| 126 | + 15 KTJDG, 12 BHXH => 5 XCVML |
| 127 | + 3 BHXH, 2 VRPVC => 7 MZWV |
| 128 | + 121 ORE => 7 VRPVC |
| 129 | + 7 XCVML => 6 RJRHP |
| 130 | + 5 BHXH, 4 VRPVC => 5 LTCX |
| 131 | + "; |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn it_gets_same_same_results_as_the_first_examples() { |
| 135 | + assert_eq!(part_01(&parse_input(&EXAMPLE_01)), 165); |
| 136 | + assert_eq!(part_01(&parse_input(&EXAMPLE_02)), 13312); |
| 137 | + assert_eq!(part_01(&parse_input(&EXAMPLE_03)), 180697); |
| 138 | + assert_eq!(part_01(&parse_input(&EXAMPLE_04)), 2210736); |
| 139 | + } |
| 140 | +} |
0 commit comments