-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
136 lines (122 loc) · 3.59 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Problem: https://adventofcode.com/2018/day/4
use itertools::Itertools;
use sscanf::sscanf;
use std::collections::{HashMap, HashSet};
#[derive(Debug)]
struct SleepCycle {
guard: usize,
start: usize,
end: usize,
}
#[derive(Debug)]
enum Event {
BeginsShift(usize),
FallsAsleep,
WakesUp,
}
#[derive(Debug)]
struct Entry {
minute: usize,
event: Event,
}
impl Entry {
fn parse(line: &str) -> Self {
Entry {
minute: line[15..17].parse().unwrap(),
event: match &line[19..] {
"falls asleep" => Event::FallsAsleep,
"wakes up" => Event::WakesUp,
_ => {
Event::BeginsShift(sscanf!(&line[19..], "Guard #{usize} begins shift").unwrap())
}
},
}
}
}
fn parse_sleep_cycles(entries: &[Entry]) -> Vec<SleepCycle> {
let mut cycles: Vec<SleepCycle> = Vec::new();
let mut cur_guard = None;
let mut start = 0;
for Entry { minute, event } in entries.iter() {
match event {
Event::BeginsShift(guard) => {
cur_guard = Some(*guard);
}
Event::FallsAsleep => {
start = *minute;
}
Event::WakesUp => {
cycles.push(SleepCycle {
guard: cur_guard.unwrap(),
start,
end: *minute,
});
}
}
}
cycles
}
fn parse_input(input: &str) -> Vec<SleepCycle> {
let entries: Vec<_> = input.lines().sorted().map(Entry::parse).collect();
parse_sleep_cycles(&entries)
}
fn part1(cycles: &[SleepCycle]) -> usize {
let mut guards: HashMap<usize, usize> = HashMap::new();
for SleepCycle { guard, start, end } in cycles.iter() {
*guards.entry(*guard).or_default() += end - start;
}
let (best_guard, _) = guards.iter().max_by(|a, b| a.1.cmp(b.1)).unwrap();
let mut minutes: HashMap<usize, usize> = HashMap::new();
for SleepCycle { guard, start, end } in cycles.iter() {
if guard == best_guard {
for m in *start..*end {
*minutes.entry(m).or_default() += 1;
}
}
}
minutes
.iter()
.max_by(|a, b| a.1.cmp(b.1))
.map(|(best_minute, _)| best_guard * best_minute)
.unwrap()
}
fn best_minute_for_guard(the_guard: usize, cycles: &[SleepCycle]) -> Option<(usize, usize, usize)> {
let mut minutes: HashMap<usize, usize> = HashMap::new();
for SleepCycle { guard, start, end } in cycles.iter() {
if *guard == the_guard {
for m in *start..*end {
*minutes.entry(m).or_default() += 1;
}
}
}
if let Some((best_minute, amount)) = minutes.iter().max_by(|a, b| a.1.cmp(b.1)) {
Some((the_guard, *best_minute, *amount))
} else {
None
}
}
fn part2(cycles: &[SleepCycle]) -> usize {
let mut guards = HashSet::new();
for SleepCycle { guard, .. } in cycles.iter() {
guards.insert(*guard);
}
guards
.iter()
.filter_map(|g| best_minute_for_guard(*g, cycles))
.max_by(|&(_, _, a), &(_, _, b)| a.cmp(&b))
.map(|(best_guard, best_minute, _)| best_guard * best_minute)
.unwrap()
}
fn main() {
let cycles = parse_input(include_str!("input.txt"));
println!("Part 1: {:?}", part1(&cycles));
println!("Part 2: {:?}", part2(&cycles));
}
#[test]
fn test_part1() {
assert_eq!(part1(&parse_input(include_str!("test.txt"))), 240);
}
#[test]
fn test_part2() {
assert_eq!(part2(&parse_input(include_str!("test.txt"))), 4455);
}