didn't commit the project files the first time :^P

This commit is contained in:
2022-12-06 00:53:47 +01:00
parent 20095d6ac6
commit 77c6d273b9
3 changed files with 1064 additions and 0 deletions

9
day_4/Cargo.toml Normal file
View 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

File diff suppressed because it is too large Load Diff

55
day_4/src/main.rs Normal file
View 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(&current_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);
}