Advertisement
krzysz00

Advent of code, day 24

Dec 25th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.59 KB | None | 0 0
  1. use std::io::{stdin,BufRead};
  2. use std::collections::{HashMap,HashSet};
  3. use std::cmp::{Ordering, max};
  4.  
  5. type Component = (u32, u32);
  6. // No duplicate compoments, we're safe
  7. type Inventory = HashMap<u32, Vec<Component>>;
  8. type Bridge = HashSet<Component>;
  9.  
  10. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
  11. struct Solution {
  12.     pub max_str: u32,
  13.     pub max_depth: u32,
  14.     pub deep_str: u32,
  15. }
  16.  
  17. fn solve(inv: &Inventory, bridge: &mut Bridge,
  18.          strength: u32, ending: u32, depth: u32) -> Solution {
  19.     let recursion: Vec<Component> = match inv.get(&ending) {
  20.         Some(continues) =>
  21.             continues.iter().filter(|c| !bridge.contains(c))
  22.             .cloned().collect(),
  23.         None => Vec::new()
  24.     };
  25.     let mut ret = Solution { max_str: strength,
  26.                              max_depth: depth,
  27.                              deep_str: strength, };
  28.     for c in recursion {
  29.         bridge.insert(c);
  30.         let new_end = if c.0 == ending { c.1 } else { c.0 };
  31.         let their_solution = solve(inv, bridge, strength + c.0 + c.1, new_end,
  32.                                    depth + 1);
  33.         bridge.remove(&c);
  34.  
  35.         ret.max_str = max(ret.max_str, their_solution.max_str);
  36.         match their_solution.max_depth.cmp(&ret.max_depth) {
  37.             Ordering::Greater => {
  38.                 ret.max_depth = their_solution.max_depth;
  39.                 ret.deep_str = their_solution.deep_str;
  40.             },
  41.             Ordering:: Equal => {
  42.                 ret.deep_str = max(ret.deep_str, their_solution.deep_str);
  43.             },
  44.             Ordering::Less => (),
  45.         }
  46.     }
  47.     ret
  48. }
  49.  
  50. fn parse() -> Inventory {
  51.     let stdin = stdin();
  52.     let handle = stdin.lock();
  53.  
  54.     let mut ret = HashMap::new();
  55.     for line in handle.lines() {
  56.         let line = line.expect("I/O error");
  57.         let mut number_iter =
  58.             line.split('/').map(|x| x.parse().expect("Not an int"));
  59.         let a = number_iter.next().expect("Not enough input");
  60.         let b = number_iter.next().expect("Not enough input");
  61.  
  62.         {
  63.             let a_entry = ret.entry(a);
  64.             let a_vec = a_entry.or_insert_with(Vec::new);
  65.             a_vec.push((a, b));
  66.         }
  67.         {
  68.             let b_entry = ret.entry(b);
  69.             let b_vec = b_entry.or_insert_with(Vec::new);
  70.             b_vec.push((a, b));
  71.         }
  72.     }
  73.     ret
  74. }
  75.  
  76. fn main() {
  77.     let inventory = parse();
  78.     let mut bridge_holder = HashSet::new();
  79.  
  80.     let solution = solve(&inventory, &mut bridge_holder,
  81.                          0, 0, 0);
  82.     println!("{:?}", solution);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement