Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::io::{BufRead, BufReader};
- use std::collections::HashMap;
- use std::fs::File;
- use regex::Regex;
- fn main() {
- let input_data = get_input_data
- (&String::from("/mnt/c/Users/bylow/Downloads/input-dec10.txt"));
- let stop_index = get_last_valid_index(&input_data);
- let prod_diff = sum_difference(&input_data, stop_index.unwrap());
- println!("index: {}", prod_diff);
- let number_of_alternatives = get_number_of_alternatives(&input_data, stop_index.unwrap());
- println!("Number of alternatives: {}", number_of_alternatives+1);
- }
- fn get_number_of_alternatives(data: &Vec<usize>, stop_index: usize) -> usize {
- let chain = get_start_chain(&data, stop_index);
- let mut memory: HashMap<usize, usize> = HashMap::new();
- return count_sub_seq(&chain, 0, &mut memory);
- }
- fn count_sub_seq(data: &Vec<usize>, start_index: usize, memory: &mut HashMap<usize,usize>) -> usize {
- let mut sum = 0;
- for idx in start_index..data.len() {
- if idx + 2 < data.len() && data[idx+2] - data[idx] <= 3 {
- sum += 1;
- let key: usize = idx + 2;
- update_memory(memory, key, data);
- sum += memory.get(&key).unwrap();
- }
- if idx + 3 < data.len() && data[idx+3] - data[idx] <= 3 {
- sum += 1;
- let key: usize = idx + 3;
- update_memory(memory, key, data);
- sum += memory.get(&key).unwrap();
- }
- }
- return sum;
- }
- fn update_memory(memory: &mut HashMap<usize, usize>, key: usize, data: &Vec<usize>) {
- if !memory.contains_key(&key) {
- let number_of_subsequences = count_sub_seq(data, key, memory);
- memory.insert(key, number_of_subsequences);
- }
- }
- fn get_start_chain(data: &Vec<usize>, stop_index: usize) -> Vec<usize> {
- let mut chain: Vec<usize> = Vec::new();
- chain.push(0);
- for idx in 0..=stop_index {
- chain.push(data[idx]);
- }
- return chain;
- }
- fn sum_difference(data: &Vec<usize>, stop_index: usize) -> usize {
- let mut diff_1 = 0;
- let mut diff_3 = 0;
- if data[0] == 1 {
- diff_1 += 1;
- }
- if data[0] == 3 {
- diff_3 += 1;
- }
- for idx in 1..=stop_index {
- let diff = data[idx] - data[idx-1];
- if diff == 1 {
- diff_1 += 1;
- }
- if diff == 3 {
- diff_3 += 1;
- }
- }
- diff_3 += 1;
- println!("diff_1 {} diff_3 {}", diff_1, diff_3);
- return diff_1 * diff_3;
- }
- fn get_last_valid_index(data: &Vec<usize>) -> Option<usize> {
- let mut start_joltage = 0;
- for (index, joltage) in data.iter().enumerate() {
- if joltage - start_joltage > 3 {
- return Some(index-1 as usize);
- }
- start_joltage = *joltage;
- }
- return Some(data.len()-1);
- }
- fn get_input_data(file_name: &String) -> Vec<usize> {
- let file = File::open(file_name)
- .expect("Could not open file");
- let buf_reader = BufReader::new(file);
- let mut data: Vec<usize> = Vec::new();
- for line in buf_reader.lines() {
- let regex = Regex::new(r"(\d+)").unwrap();
- let row: String = line.unwrap();
- let capture = regex.captures(&row).unwrap();
- let value: usize = capture[1].parse().expect("Couldn't pass i64");
- data.push(value);
- }
- data.sort();
- return data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement