Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::io::{stdin,BufRead};
- use std::collections::{HashMap,HashSet};
- use std::cmp::{Ordering, max};
- type Component = (u32, u32);
- // No duplicate compoments, we're safe
- type Inventory = HashMap<u32, Vec<Component>>;
- type Bridge = HashSet<Component>;
- #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
- struct Solution {
- pub max_str: u32,
- pub max_depth: u32,
- pub deep_str: u32,
- }
- fn solve(inv: &Inventory, bridge: &mut Bridge,
- strength: u32, ending: u32, depth: u32) -> Solution {
- let recursion: Vec<Component> = match inv.get(&ending) {
- Some(continues) =>
- continues.iter().filter(|c| !bridge.contains(c))
- .cloned().collect(),
- None => Vec::new()
- };
- let mut ret = Solution { max_str: strength,
- max_depth: depth,
- deep_str: strength, };
- for c in recursion {
- bridge.insert(c);
- let new_end = if c.0 == ending { c.1 } else { c.0 };
- let their_solution = solve(inv, bridge, strength + c.0 + c.1, new_end,
- depth + 1);
- bridge.remove(&c);
- ret.max_str = max(ret.max_str, their_solution.max_str);
- match their_solution.max_depth.cmp(&ret.max_depth) {
- Ordering::Greater => {
- ret.max_depth = their_solution.max_depth;
- ret.deep_str = their_solution.deep_str;
- },
- Ordering:: Equal => {
- ret.deep_str = max(ret.deep_str, their_solution.deep_str);
- },
- Ordering::Less => (),
- }
- }
- ret
- }
- fn parse() -> Inventory {
- let stdin = stdin();
- let handle = stdin.lock();
- let mut ret = HashMap::new();
- for line in handle.lines() {
- let line = line.expect("I/O error");
- let mut number_iter =
- line.split('/').map(|x| x.parse().expect("Not an int"));
- let a = number_iter.next().expect("Not enough input");
- let b = number_iter.next().expect("Not enough input");
- {
- let a_entry = ret.entry(a);
- let a_vec = a_entry.or_insert_with(Vec::new);
- a_vec.push((a, b));
- }
- {
- let b_entry = ret.entry(b);
- let b_vec = b_entry.or_insert_with(Vec::new);
- b_vec.push((a, b));
- }
- }
- ret
- }
- fn main() {
- let inventory = parse();
- let mut bridge_holder = HashSet::new();
- let solution = solve(&inventory, &mut bridge_holder,
- 0, 0, 0);
- println!("{:?}", solution);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement