ElTriunfador

dec 10

Dec 12th, 2020
770
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::io::{BufRead, BufReader};
  2. use std::collections::HashMap;
  3. use std::fs::File;
  4. use regex::Regex;
  5.  
  6. fn main() {
  7.     let input_data = get_input_data
  8.         (&String::from("/mnt/c/Users/bylow/Downloads/input-dec10.txt"));
  9.     let stop_index = get_last_valid_index(&input_data);
  10.     let prod_diff = sum_difference(&input_data, stop_index.unwrap());
  11.     println!("index: {}", prod_diff);
  12.     let number_of_alternatives = get_number_of_alternatives(&input_data, stop_index.unwrap());
  13.     println!("Number of alternatives: {}", number_of_alternatives+1);
  14. }
  15.  
  16. fn get_number_of_alternatives(data: &Vec<usize>, stop_index: usize) -> usize {
  17.     let chain = get_start_chain(&data, stop_index);
  18.     let mut memory: HashMap<usize, usize> = HashMap::new();
  19.     return count_sub_seq(&chain, 0, &mut memory);
  20. }
  21.  
  22. fn count_sub_seq(data: &Vec<usize>, start_index: usize, memory: &mut HashMap<usize,usize>) -> usize {
  23.     let mut sum = 0;
  24.     for idx in start_index..data.len() {
  25.         if idx + 2 < data.len() && data[idx+2] - data[idx] <= 3 {
  26.             sum += 1;
  27.             let key: usize = idx + 2;
  28.             update_memory(memory, key, data);
  29.             sum += memory.get(&key).unwrap();
  30.         }
  31.         if idx + 3 < data.len() && data[idx+3] - data[idx] <= 3 {
  32.             sum += 1;
  33.             let key: usize = idx + 3;
  34.             update_memory(memory, key, data);
  35.             sum += memory.get(&key).unwrap();
  36.         }
  37.     }
  38.     return sum;
  39. }
  40.  
  41. fn update_memory(memory: &mut HashMap<usize, usize>, key: usize, data: &Vec<usize>) {
  42.     if !memory.contains_key(&key) {
  43.         let number_of_subsequences = count_sub_seq(data, key, memory);
  44.         memory.insert(key, number_of_subsequences);
  45.     }
  46. }
  47.  
  48. fn get_start_chain(data: &Vec<usize>, stop_index: usize) -> Vec<usize> {
  49.     let mut chain: Vec<usize> = Vec::new();
  50.     chain.push(0);
  51.     for idx in 0..=stop_index {
  52.         chain.push(data[idx]);
  53.     }
  54.     return chain;
  55. }
  56.  
  57. fn sum_difference(data: &Vec<usize>, stop_index: usize) -> usize {
  58.     let mut diff_1 = 0;
  59.     let mut diff_3 = 0;
  60.     if data[0] == 1 {
  61.         diff_1 += 1;
  62.     }
  63.     if data[0] == 3 {
  64.         diff_3 += 1;
  65.     }
  66.     for idx in 1..=stop_index {
  67.         let diff = data[idx] - data[idx-1];
  68.         if diff == 1 {
  69.             diff_1 += 1;
  70.         }
  71.         if diff == 3 {
  72.             diff_3 += 1;
  73.         }
  74.     }
  75.     diff_3 += 1;
  76.     println!("diff_1 {} diff_3 {}", diff_1, diff_3);
  77.     return diff_1 * diff_3;
  78. }
  79.  
  80. fn get_last_valid_index(data: &Vec<usize>) -> Option<usize> {
  81.     let mut start_joltage = 0;
  82.     for (index, joltage) in data.iter().enumerate() {
  83.         if joltage - start_joltage > 3 {
  84.             return Some(index-1 as usize);
  85.         }
  86.         start_joltage = *joltage;
  87.     }
  88.     return Some(data.len()-1);
  89. }
  90.  
  91. fn get_input_data(file_name: &String) -> Vec<usize> {
  92.     let file = File::open(file_name)
  93.         .expect("Could not open file");
  94.     let buf_reader = BufReader::new(file);
  95.     let mut data: Vec<usize> = Vec::new();
  96.     for line in buf_reader.lines() {
  97.         let regex = Regex::new(r"(\d+)").unwrap();
  98.         let row: String = line.unwrap();
  99.         let capture = regex.captures(&row).unwrap();
  100.         let value: usize = capture[1].parse().expect("Couldn't pass i64");
  101.         data.push(value);
  102.     }
  103.     data.sort();
  104.     return data;
  105. }
  106.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×