day4 done, but code needs to be refactored
This commit is contained in:
parent
2eb8adf620
commit
fa6bd59445
4
day4/Cargo.lock
generated
Normal file
4
day4/Cargo.lock
generated
Normal file
@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "day4"
|
||||
version = "0.1.0"
|
||||
|
6
day4/Cargo.toml
Normal file
6
day4/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day4"
|
||||
version = "0.1.0"
|
||||
authors = ["attila <daf276@gmx.de>"]
|
||||
|
||||
[dependencies]
|
1026
day4/input
Normal file
1026
day4/input
Normal file
File diff suppressed because it is too large
Load Diff
76
day4/src/main.rs
Normal file
76
day4/src/main.rs
Normal file
@ -0,0 +1,76 @@
|
||||
use std::fs;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn main() {
|
||||
let file = fs::read_to_string("input").expect("Unable to read file");
|
||||
let mut lines: Vec<&str> = file.split("\n").collect();
|
||||
lines.sort();
|
||||
|
||||
let mut times = HashMap::new();
|
||||
let mut guards = HashMap::new();
|
||||
|
||||
let mut start = 0;
|
||||
let mut end = 0;
|
||||
let mut guard = 0;
|
||||
|
||||
for line in lines {
|
||||
let r = line.split(|c| c == '-' || c == ' ' || c == ':' || c == ':' || c == ']').collect::<Vec<&str>>();
|
||||
|
||||
|
||||
if r[7].contains("#") {
|
||||
let number = r[7].split("#").collect::<Vec<&str>>();
|
||||
guard = number[1].parse::<i32>().unwrap();
|
||||
}
|
||||
|
||||
if r[7] == "asleep" {
|
||||
start = r[4].parse::<i32>().unwrap();
|
||||
}
|
||||
|
||||
if r[7] == "up" {
|
||||
end = r[4].parse::<i32>().unwrap();
|
||||
|
||||
*times.entry(guard).or_insert(end - start) += end - start;
|
||||
|
||||
for i in start..end {
|
||||
*guards.entry((guard, i)).or_insert(0) +=1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//Part1
|
||||
let max_time_asleep = *times.values().into_iter().max().unwrap();
|
||||
let mut guard_who_slept_most = 0;
|
||||
|
||||
for (key, value) in times {
|
||||
if value == max_time_asleep {
|
||||
guard_who_slept_most = key;
|
||||
}
|
||||
}
|
||||
|
||||
let mut minute_most_asleep = 0;
|
||||
let mut times_asleep_in_same_minute = 0;
|
||||
|
||||
for ((guard, minute), no_times_asleep_in_same_minute) in &guards{
|
||||
if guard == &guard_who_slept_most && no_times_asleep_in_same_minute >= ×_asleep_in_same_minute{
|
||||
times_asleep_in_same_minute = *no_times_asleep_in_same_minute;
|
||||
minute_most_asleep = *minute;
|
||||
}
|
||||
}
|
||||
|
||||
//Part2
|
||||
let mut max_no_times_asleep_in_same_minute = 0;
|
||||
let mut final_minute= 0;
|
||||
let mut test = 0;
|
||||
|
||||
for ((guard, minute), no_times_asleep_in_same_minute) in &guards{
|
||||
if no_times_asleep_in_same_minute > &max_no_times_asleep_in_same_minute {
|
||||
max_no_times_asleep_in_same_minute = *no_times_asleep_in_same_minute;
|
||||
final_minute = *minute;
|
||||
test = *guard;
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", minute_most_asleep as i32 *guard_who_slept_most);
|
||||
println!("{}", test*final_minute);
|
||||
}
|
Loading…
Reference in New Issue
Block a user