daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern crate itertools;
  2.  
  3. use itertools::Itertools;
  4.  
  5. fn get_pins(observed: &str) -> Vec<String> {
  6.     let combos = vec![
  7.         vec![0, 8],
  8.         vec![1, 2, 4],
  9.         vec![2, 1, 3, 5],
  10.         vec![3, 2, 6],
  11.         vec![4, 1, 5, 7],
  12.         vec![5, 2, 4, 6, 8],
  13.         vec![6, 3, 5, 9],
  14.         vec![7, 4, 8],
  15.         vec![8, 0, 5, 9, 7],
  16.         vec![9, 8, 6],
  17.         vec![0, 8],
  18.     ];
  19.     let digits: Vec<_> = observed.chars().map(|c| c.to_digit(10).unwrap()).collect();
  20.     let mut possible = vec![];
  21.     for n in digits {
  22.         possible.push(&combos[n as usize]);
  23.     }
  24.     let mut ret: Vec<String> = possible[0].iter().map(|x| x.to_string()).collect();
  25.     for p in 1..possible.len() {
  26.         let r: Vec<_> = ret
  27.             .clone()
  28.             .iter()
  29.             .cartesian_product(possible[p].iter())
  30.             .fold(vec![], |mut vec, (ref a, ref b)| {
  31.                 vec.push(format!("{}{}", a, b));
  32.                 vec
  33.             });
  34.         ret = r;
  35.     }
  36.     ret
  37. }
  38.  
  39. #[test]
  40. fn length_test() {
  41.     let v = vec![
  42.         ("8", vec!["5", "7", "8", "9", "0"]),
  43.         (
  44.             "11",
  45.             vec!["11", "22", "44", "12", "21", "14", "41", "24", "42"],
  46.         ),
  47.         (
  48.             "369",
  49.             vec![
  50.                 "339", "366", "399", "658", "636", "258", "268", "669", "668", "266", "369", "398",
  51.                 "256", "296", "259", "368", "638", "396", "238", "356", "659", "639", "666", "359",
  52.                 "336", "299", "338", "696", "269", "358", "656", "698", "699", "298", "236", "239",
  53.             ],
  54.         ),
  55.     ];
  56.     for (num, exp) in v {
  57.         assert_eq!(get_pins(num).len(), exp.len())
  58.     }
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top