You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.6 KiB
Rust
84 lines
2.6 KiB
Rust
#![crate_name = "advent_of_code_day4"]
|
|
|
|
//use std::env;
|
|
use std::fs;
|
|
use std::path::{Path};
|
|
|
|
fn parse_sectors(input: &str) -> Vec<Vec<Vec<u32>>> {
|
|
/// 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<Vec<u32>>> = 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<u32>> = Vec::new();
|
|
|
|
// str_range_exp = "1-3"
|
|
|
|
let str_range_params: Vec<&str> = secs_str[0].split("-").collect();
|
|
let mut range: Vec<u32> = Vec::new();
|
|
for i in str_range_params[0].parse::<u32>().unwrap()..(str_range_params[1].parse::<u32>().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<u32> = Vec::new();
|
|
for i in str_range_params[0].parse::<u32>().unwrap()..(str_range_params[1].parse::<u32>().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<u32>>) -> Vec<u32> {
|
|
/// 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<u32> = 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<Vec<Vec<u32>>> = parse_sectors(&contents);
|
|
let mut complete_overlap_count = 0;
|
|
|
|
for pair in §or_pairs {
|
|
let overlaps: Vec<u32> = 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);
|
|
} |