Day 4 (Puzzle 1) - Rucksack Reorganization
Decided to use Rust for this and boy did it take hours... It is a very simplistic program without any fancy whistles. But it runs and succeeded in the puzzle!main
parent
2c44d2b9bb
commit
c7ffe2b2d1
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "advent_of_code_day4"
|
||||
version = "0.0.1"
|
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "advent_of_code_day4"
|
||||
version = "0.0.1"
|
||||
authors = ["Peery"]
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
@ -0,0 +1 @@
|
||||
2-4,6-8
|
@ -0,0 +1,6 @@
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,84 @@
|
||||
#![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() == pair[0].len() || overlaps.len() == pair[1].len() {
|
||||
println!("Found a complete overlap in the following pair: {:?}", pair);
|
||||
complete_overlap_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!();
|
||||
println!("Found {} pairs with complete overlaps!", complete_overlap_count);
|
||||
}
|
Loading…
Reference in New Issue