|
@@ -0,0 +1,206 @@
|
|
1
|
+extern crate chrono;
|
|
2
|
+extern crate regex;
|
|
3
|
+
|
|
4
|
+use std::error::Error;
|
|
5
|
+use std::fs::File;
|
|
6
|
+use std::io::{BufRead, BufReader};
|
|
7
|
+use std::fmt;
|
|
8
|
+use std::collections::{HashMap, HashSet};
|
|
9
|
+use std::iter::FromIterator;
|
|
10
|
+
|
|
11
|
+use chrono::prelude::*;
|
|
12
|
+use regex::{Regex, Captures};
|
|
13
|
+
|
|
14
|
+const INPUT: &str = "inputs/4.txt";
|
|
15
|
+
|
|
16
|
+#[derive(Debug, PartialEq)]
|
|
17
|
+enum Record {
|
|
18
|
+ Start {
|
|
19
|
+ time: NaiveDateTime,
|
|
20
|
+ guard_id: u32,
|
|
21
|
+ },
|
|
22
|
+ Sleep {
|
|
23
|
+ time: NaiveDateTime,
|
|
24
|
+ },
|
|
25
|
+ Wake {
|
|
26
|
+ time: NaiveDateTime,
|
|
27
|
+ },
|
|
28
|
+}
|
|
29
|
+
|
|
30
|
+#[derive(Debug, Clone, PartialEq)]
|
|
31
|
+struct MalformedRecord {
|
|
32
|
+ details: String
|
|
33
|
+}
|
|
34
|
+
|
|
35
|
+impl MalformedRecord {
|
|
36
|
+ fn new(msg: &str) -> MalformedRecord {
|
|
37
|
+ MalformedRecord{ details: msg.to_string() }
|
|
38
|
+ }
|
|
39
|
+}
|
|
40
|
+
|
|
41
|
+impl fmt::Display for MalformedRecord {
|
|
42
|
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
43
|
+ write!(f, "{}", self.details)
|
|
44
|
+ }
|
|
45
|
+}
|
|
46
|
+
|
|
47
|
+impl Error for MalformedRecord {
|
|
48
|
+ fn description(&self) -> &str {
|
|
49
|
+ &self.details
|
|
50
|
+ }
|
|
51
|
+}
|
|
52
|
+
|
|
53
|
+fn read_records(filename: &str) -> Result<Vec<Record>, Box<Error>> {
|
|
54
|
+ let mut records: Vec<Record> = Vec::new();
|
|
55
|
+ let record_regex =
|
|
56
|
+ Regex::new(r"\[(?P<timestamp>\d{4}-\d{2}-\d{2}\s\d{2}:\d{2})\]\s(?:(?P<start>Guard #(?P<guard_id>\d+) begins shift)|(?P<sleep>falls asleep)|(?P<wake>wakes up))")?;
|
|
57
|
+ let file = File::open(filename)?;
|
|
58
|
+ for line in BufReader::new(file).lines() {
|
|
59
|
+ match record_regex.captures(&line?) {
|
|
60
|
+ Some(captures) => {
|
|
61
|
+ println!("{:?}", NaiveDateTime::parse_from_str(
|
|
62
|
+ &get_captured_field(&captures, "timestamp")?,
|
|
63
|
+ "%Y-%m-%d %H:%M")?);
|
|
64
|
+ if has_captured_field(&captures, "start")? {
|
|
65
|
+ records.push(Record::Start {
|
|
66
|
+ time: NaiveDateTime::parse_from_str(
|
|
67
|
+ &get_captured_field(&captures, "timestamp")?,
|
|
68
|
+ "%Y-%m-%d %H:%M")?,
|
|
69
|
+ guard_id: get_captured_field(&captures, "guard_id")?.parse()?,
|
|
70
|
+ });
|
|
71
|
+ } else if has_captured_field(&captures, "sleep")? {
|
|
72
|
+ records.push(Record::Sleep {
|
|
73
|
+ time: NaiveDateTime::parse_from_str(
|
|
74
|
+ &get_captured_field(&captures, "timestamp")?,
|
|
75
|
+ "%Y-%m-%d %H:%M")?,
|
|
76
|
+ });
|
|
77
|
+ } else {
|
|
78
|
+ records.push(Record::Wake {
|
|
79
|
+ time: NaiveDateTime::parse_from_str(
|
|
80
|
+ &get_captured_field(&captures, "timestamp")?,
|
|
81
|
+ "%Y-%m-%d %H:%M")?,
|
|
82
|
+ });
|
|
83
|
+ }
|
|
84
|
+ },
|
|
85
|
+ None => return Err(Box::new(MalformedRecord {
|
|
86
|
+ details: "Malformed record line, no fields could be found".to_string()
|
|
87
|
+ })),
|
|
88
|
+ };
|
|
89
|
+ }
|
|
90
|
+ Ok(records)
|
|
91
|
+}
|
|
92
|
+
|
|
93
|
+fn get_captured_field(captures: &Captures, field: &str) -> Result<String, Box<Error>> {
|
|
94
|
+ match captures.name(field) {
|
|
95
|
+ Some(capture) => Ok(String::from(capture.as_str())),
|
|
96
|
+ None => return Err(Box::new(MalformedRecord {
|
|
97
|
+ details: format!("Malformed record line, field {} could not be found", field)
|
|
98
|
+ }))
|
|
99
|
+ }
|
|
100
|
+}
|
|
101
|
+
|
|
102
|
+fn has_captured_field(captures: &Captures, field: &str) -> Result<bool, Box<Error>> {
|
|
103
|
+ match captures.name(field) {
|
|
104
|
+ Some(_) => Ok(true),
|
|
105
|
+ None => Ok(false)
|
|
106
|
+ }
|
|
107
|
+}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+#[cfg(test)]
|
|
111
|
+mod tests {
|
|
112
|
+ use super::*;
|
|
113
|
+
|
|
114
|
+ const TEST_INPUT: &str = "inputs/4_test.txt";
|
|
115
|
+ const TEST_INPUT_MALFORMED: &str = "inputs/4_test_malformed.txt";
|
|
116
|
+
|
|
117
|
+ #[test]
|
|
118
|
+ fn reads_records_file() {
|
|
119
|
+ assert_eq!(read_records(TEST_INPUT).unwrap(), vec![
|
|
120
|
+ Record::Start {
|
|
121
|
+ time: NaiveDateTime::parse_from_str(
|
|
122
|
+ "1518-11-01 00:00", "%Y-%m-%d %H:%M").unwrap(),
|
|
123
|
+ guard_id: 10,
|
|
124
|
+ },
|
|
125
|
+ Record::Sleep {
|
|
126
|
+ time: NaiveDateTime::parse_from_str(
|
|
127
|
+ "1518-11-01 00:05", "%Y-%m-%d %H:%M").unwrap(),
|
|
128
|
+ },
|
|
129
|
+ Record::Wake {
|
|
130
|
+ time: NaiveDateTime::parse_from_str(
|
|
131
|
+ "1518-11-01 00:25", "%Y-%m-%d %H:%M").unwrap(),
|
|
132
|
+ },
|
|
133
|
+ Record::Sleep {
|
|
134
|
+ time: NaiveDateTime::parse_from_str(
|
|
135
|
+ "1518-11-01 00:30", "%Y-%m-%d %H:%M").unwrap(),
|
|
136
|
+ },
|
|
137
|
+ Record::Wake {
|
|
138
|
+ time: NaiveDateTime::parse_from_str(
|
|
139
|
+ "1518-11-01 00:55", "%Y-%m-%d %H:%M").unwrap(),
|
|
140
|
+ },
|
|
141
|
+ Record::Start {
|
|
142
|
+ time: NaiveDateTime::parse_from_str(
|
|
143
|
+ "1518-11-01 23:58", "%Y-%m-%d %H:%M").unwrap(),
|
|
144
|
+ guard_id: 99,
|
|
145
|
+ },
|
|
146
|
+ Record::Sleep {
|
|
147
|
+ time: NaiveDateTime::parse_from_str(
|
|
148
|
+ "1518-11-02 00:40", "%Y-%m-%d %H:%M").unwrap(),
|
|
149
|
+ },
|
|
150
|
+ Record::Wake {
|
|
151
|
+ time: NaiveDateTime::parse_from_str(
|
|
152
|
+ "1518-11-02 00:50", "%Y-%m-%d %H:%M").unwrap(),
|
|
153
|
+ },
|
|
154
|
+ Record::Start {
|
|
155
|
+ time: NaiveDateTime::parse_from_str(
|
|
156
|
+ "1518-11-03 00:05", "%Y-%m-%d %H:%M").unwrap(),
|
|
157
|
+ guard_id: 10,
|
|
158
|
+ },
|
|
159
|
+ Record::Sleep {
|
|
160
|
+ time: NaiveDateTime::parse_from_str(
|
|
161
|
+ "1518-11-03 00:24", "%Y-%m-%d %H:%M").unwrap(),
|
|
162
|
+ },
|
|
163
|
+ Record::Wake {
|
|
164
|
+ time: NaiveDateTime::parse_from_str(
|
|
165
|
+ "1518-11-03 00:29", "%Y-%m-%d %H:%M").unwrap(),
|
|
166
|
+ },
|
|
167
|
+ Record::Start {
|
|
168
|
+ time: NaiveDateTime::parse_from_str(
|
|
169
|
+ "1518-11-04 00:02", "%Y-%m-%d %H:%M").unwrap(),
|
|
170
|
+ guard_id: 99,
|
|
171
|
+ },
|
|
172
|
+ Record::Sleep {
|
|
173
|
+ time: NaiveDateTime::parse_from_str(
|
|
174
|
+ "1518-11-04 00:36", "%Y-%m-%d %H:%M").unwrap(),
|
|
175
|
+ },
|
|
176
|
+ Record::Wake {
|
|
177
|
+ time: NaiveDateTime::parse_from_str(
|
|
178
|
+ "1518-11-04 00:46", "%Y-%m-%d %H:%M").unwrap(),
|
|
179
|
+ },
|
|
180
|
+ Record::Start {
|
|
181
|
+ time: NaiveDateTime::parse_from_str(
|
|
182
|
+ "1518-11-05 00:03", "%Y-%m-%d %H:%M").unwrap(),
|
|
183
|
+ guard_id: 99,
|
|
184
|
+ },
|
|
185
|
+ Record::Sleep {
|
|
186
|
+ time: NaiveDateTime::parse_from_str(
|
|
187
|
+ "1518-11-05 00:45", "%Y-%m-%d %H:%M").unwrap(),
|
|
188
|
+ },
|
|
189
|
+ Record::Wake {
|
|
190
|
+ time: NaiveDateTime::parse_from_str(
|
|
191
|
+ "1518-11-05 00:55", "%Y-%m-%d %H:%M").unwrap(),
|
|
192
|
+ },
|
|
193
|
+ ]);
|
|
194
|
+ }
|
|
195
|
+
|
|
196
|
+ #[test]
|
|
197
|
+ fn errors_on_malformed_records_file() {
|
|
198
|
+ match read_records(TEST_INPUT_MALFORMED) {
|
|
199
|
+ Ok(_) => assert!(false, "read_records should have returned an error"),
|
|
200
|
+ Err(err) => assert_eq!(
|
|
201
|
+ (*err).description(),
|
|
202
|
+ "Malformed record line, no fields could be found".to_string(),
|
|
203
|
+ ),
|
|
204
|
+ }
|
|
205
|
+ }
|
|
206
|
+}
|