#![crate_name = "advent_of_code_day4"] //use std::env; use std::fs; use std::path::{Path}; fn parse_sectors(input: &str) -> Vec>> { /// Parse the given string for the sector starts and ends. /// /// # Arguments /// * `input` - A string directly read from the file assert!(!input.is_empty()); let lines: Vec<&str> = input.split("\n").collect(); let mut sector_groups: Vec>> = Vec::new(); // <[<1,2,3>, <3,4,5>]> for line in &lines { let secs_str: Vec<&str> = line.split(",").collect(); // splitting into sector ranges, ["1-3", "3-5"] if secs_str.len() != 2 { continue } let mut line_vector: Vec> = Vec::new(); // str_range_exp = "1-3" let str_range_params: Vec<&str> = secs_str[0].split("-").collect(); let mut range: Vec = Vec::new(); for i in str_range_params[0].parse::().unwrap()..(str_range_params[1].parse::().unwrap()+1) { range.push(i); } line_vector.push(range.clone()); let str_range_params: Vec<&str> = secs_str[1].split("-").collect(); let mut range: Vec = Vec::new(); for i in str_range_params[0].parse::().unwrap()..(str_range_params[1].parse::().unwrap()+1) { range.push(i); } line_vector.push(range.clone()); sector_groups.push(line_vector); } //println!("My Ranges are: {:?}", sector_groups); return sector_groups; } fn check_for_overlaps(pair: &Vec>) -> Vec { /// Checks the given two lust of values for overlaps in each other. /// If there are overlaps a vector of them is returned. /// If not an empty array is returned assert!(pair.len() == 2); let mut overlaps: Vec = Vec::new(); for value in &pair[0] { if pair[1].contains(value) { overlaps.push(*value); } } return overlaps; } fn main() { let input_path = Path::new("./input/input.lst"); let contents = fs::read_to_string(input_path).unwrap(); //println!("My given file: \n{}", contents); let sector_pairs: Vec>> = parse_sectors(&contents); let mut complete_overlap_count = 0; for pair in §or_pairs { let overlaps: Vec = check_for_overlaps(&pair); //println!("Overlap is: {:?}", overlaps); if overlaps.len() != 0 { println!("Found an overlap in the following pair: {:?}", pair); complete_overlap_count += 1; } } println!(); println!("Found {} pairs with overlaps!", complete_overlap_count); }