mirror of
https://github.com/bvanroll/advent_of_code_2022.git
synced 2025-08-29 03:32:40 +00:00
didn't commit the project files the first time :^P
This commit is contained in:
9
day_4/Cargo.toml
Normal file
9
day_4/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "advent_of_code_2022"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rfd = "0.10.0"
|
1000
day_4/input
Normal file
1000
day_4/input
Normal file
File diff suppressed because it is too large
Load Diff
55
day_4/src/main.rs
Normal file
55
day_4/src/main.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
|
|
||||||
|
struct Team {
|
||||||
|
first: Assignment,
|
||||||
|
second: Assignment
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Assignment {
|
||||||
|
start: u32,
|
||||||
|
end: u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_assignment(unparsed_assignment: &str) -> Assignment {
|
||||||
|
let parts:Vec<&str> = unparsed_assignment.split("-").collect();
|
||||||
|
|
||||||
|
return Assignment {start: parts[0].clone().parse::<u32>().unwrap(), end: parts[1].clone().parse::<u32>().unwrap()}; //this won't work but internet is down :/
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_contain(check_team: &Team) -> bool {
|
||||||
|
return (check_team.first.start <= check_team.second.start && check_team.first.end >= check_team.second.end) || (check_team.second.start <= check_team.first.start && check_team.second.end >= check_team.first.end)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_overlap(check_team: &Team) -> bool {
|
||||||
|
return (check_team.first.start <= check_team.second.end && check_team.first.start >= check_team.second.start) || (check_team.second.start <= check_team.first.end && check_team.second.start >= check_team.first.start)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut contain_counter = 0;
|
||||||
|
let mut overlap_counter = 0;
|
||||||
|
let current_path = std::env::current_dir().unwrap();
|
||||||
|
let res = rfd::FileDialog::new().set_directory(¤t_path).pick_file().unwrap();
|
||||||
|
let list = File::open(res.as_path()).unwrap();
|
||||||
|
let reader = BufReader::new(list);
|
||||||
|
for buffer in reader.lines() {
|
||||||
|
if let Ok(line) = buffer {
|
||||||
|
let assignments = line.split(",").collect::<Vec<&str>>(); //NOTE the ::<... part seems to be generic rust
|
||||||
|
//Rust can also get this typing from the var it's going to put the output in
|
||||||
|
// let assignments: Vec<&str> = line.split().collect();
|
||||||
|
let mut first_assignment = parse_assignment(assignments[0]);
|
||||||
|
let mut second_assignment = parse_assignment(assignments[1]);
|
||||||
|
let mut temp_team = Team {first: first_assignment, second: second_assignment};
|
||||||
|
if check_contain(&temp_team) {
|
||||||
|
contain_counter += 1;
|
||||||
|
}
|
||||||
|
if check_overlap(&temp_team) {
|
||||||
|
overlap_counter += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("there should be {} containing teams", contain_counter);
|
||||||
|
println!("There should be {} overlapping teams", overlap_counter);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user