-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.rs
67 lines (58 loc) · 1.95 KB
/
day4.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::fs;
use std::collections::HashMap;
fn main() {
let contents = fs::read_to_string("day4.input")
.expect("Something went wrong reading the file");
let mut contents: Vec<_> = contents.lines().collect();
contents.sort();
let mut slept_total: HashMap<&str,i32> = HashMap::new();
let mut slept_minutes: HashMap<&str,[i32;60]> = HashMap::new();
let mut cur_id = "";
let mut minute_asleep = 0;
let mut max: (&str,i32) = ("",0);
for row in contents {
let row: Vec<_> = row.split(']').collect();
let parsed_minute = row[0].split_whitespace().collect::<Vec<&str>>()[1].split(':').collect::<Vec<&str>>()[1].parse::<i32>().unwrap();
if row[1].contains('#') {
cur_id = row[1].split_whitespace().collect::<Vec<&str>>()[1];
} else if row[1] == " falls asleep" {
minute_asleep = parsed_minute;
} else {
let count = slept_total.entry(cur_id).or_insert(0);
*count += parsed_minute - minute_asleep;
if *count > max.1 {
max = (cur_id, *count);
}
for i in minute_asleep..parsed_minute {
let min = slept_minutes.entry(cur_id).or_insert([0;60]);
min[i as usize] += 1;
}
}
}
// Part 1
let mut max_min_count = 0;
let mut max_min = 0;
for minutes in slept_minutes.get(max.0) {
for j in 0..minutes.len() {
if minutes[j] > max_min_count {
max_min_count = minutes[j];
max_min = j as i32;
}
}
}
println!("{:#?}", max.0.trim_start_matches('#').parse::<i32>().unwrap() * max_min);
// Part 2
let mut max_id = 0;
let mut max_min = 0;
let mut max_min_count = 0;
for (id, minutes) in slept_minutes {
for j in 0..minutes.len() {
if minutes[j] > max_min_count {
max_min_count = minutes[j];
max_min = j as i32;
max_id = id.trim_start_matches('#').parse::<i32>().unwrap();
}
}
}
println!("{:#?}", max_id * max_min);
}