|
@@ -0,0 +1,164 @@
|
|
1
|
+extern crate regex;
|
|
2
|
+
|
|
3
|
+use std::error::Error;
|
|
4
|
+use std::fs::File;
|
|
5
|
+use std::io::{BufRead, BufReader};
|
|
6
|
+use std::fmt;
|
|
7
|
+use std::collections::HashMap;
|
|
8
|
+
|
|
9
|
+use regex::{Regex, Captures};
|
|
10
|
+
|
|
11
|
+const INPUT: &str = "inputs/3.txt";
|
|
12
|
+
|
|
13
|
+#[derive(Debug, PartialEq)]
|
|
14
|
+struct Claim {
|
|
15
|
+ id: u32,
|
|
16
|
+ left: u32,
|
|
17
|
+ top: u32,
|
|
18
|
+ width: u32,
|
|
19
|
+ height: u32,
|
|
20
|
+}
|
|
21
|
+
|
|
22
|
+#[derive(Debug, PartialEq, Eq, Hash)]
|
|
23
|
+struct Point {
|
|
24
|
+ x: u32,
|
|
25
|
+ y: u32,
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+#[derive(Debug, Clone, PartialEq)]
|
|
29
|
+struct MalformedClaim {
|
|
30
|
+ details: String
|
|
31
|
+}
|
|
32
|
+
|
|
33
|
+impl MalformedClaim {
|
|
34
|
+ fn new(msg: &str) -> MalformedClaim {
|
|
35
|
+ MalformedClaim{ details: msg.to_string() }
|
|
36
|
+ }
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+impl fmt::Display for MalformedClaim {
|
|
40
|
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
41
|
+ write!(f, "{}", self.details)
|
|
42
|
+ }
|
|
43
|
+}
|
|
44
|
+
|
|
45
|
+impl Error for MalformedClaim {
|
|
46
|
+ fn description(&self) -> &str {
|
|
47
|
+ &self.details
|
|
48
|
+ }
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+pub fn solve_part1() -> Result<u32, Box<Error>> {
|
|
52
|
+ Ok(count_overlapping_claimed_points(read_claims(INPUT)?))
|
|
53
|
+}
|
|
54
|
+
|
|
55
|
+fn count_overlapping_claimed_points(claims: Vec<Claim>) -> u32 {
|
|
56
|
+ let mut claimed_points: HashMap<Point, u32> = HashMap::new();
|
|
57
|
+ for claim in claims {
|
|
58
|
+ for point in list_points_in_claim(claim) {
|
|
59
|
+ let current_point = claimed_points.get(&point).unwrap_or(&0);
|
|
60
|
+ claimed_points.insert(point, current_point + 1);
|
|
61
|
+ }
|
|
62
|
+ }
|
|
63
|
+ return claimed_points.values().fold(0, |acc, claims| {
|
|
64
|
+ if claims > &1 {
|
|
65
|
+ return acc + 1
|
|
66
|
+ }
|
|
67
|
+ acc
|
|
68
|
+ })
|
|
69
|
+}
|
|
70
|
+
|
|
71
|
+fn list_points_in_claim(claim: Claim) -> Vec<Point> {
|
|
72
|
+ let mut points = Vec::new();
|
|
73
|
+ for x in 0..claim.width {
|
|
74
|
+ for y in 0..claim.height {
|
|
75
|
+ points.push(Point { x: claim.left + x, y: claim.top + y });
|
|
76
|
+ }
|
|
77
|
+ }
|
|
78
|
+ points
|
|
79
|
+}
|
|
80
|
+
|
|
81
|
+fn read_claims(filename: &str) -> Result<Vec<Claim>, Box<Error>> {
|
|
82
|
+ let mut claims: Vec<Claim> = Vec::new();
|
|
83
|
+ let claim_regex =
|
|
84
|
+ Regex::new(r"#(?P<id>\d+) @ (?P<left>\d+),(?P<top>\d+): (?P<width>\d+)x(?P<height>\d+)")?;
|
|
85
|
+ let file = File::open(filename)?;
|
|
86
|
+ for line in BufReader::new(file).lines() {
|
|
87
|
+ match claim_regex.captures(&line?) {
|
|
88
|
+ Some(captures) => {
|
|
89
|
+ let claim = Claim {
|
|
90
|
+ id: get_captured_field(&captures, "id")?,
|
|
91
|
+ left: get_captured_field(&captures, "left")?,
|
|
92
|
+ top: get_captured_field(&captures, "top")?,
|
|
93
|
+ width: get_captured_field(&captures, "width")?,
|
|
94
|
+ height: get_captured_field(&captures, "height")?,
|
|
95
|
+ };
|
|
96
|
+ claims.push(claim);
|
|
97
|
+ },
|
|
98
|
+ None => return Err(Box::new(MalformedClaim {
|
|
99
|
+ details: "Malformed claim line, no fields could be found".to_string()
|
|
100
|
+ })),
|
|
101
|
+ };
|
|
102
|
+ }
|
|
103
|
+ Ok(claims)
|
|
104
|
+}
|
|
105
|
+
|
|
106
|
+fn get_captured_field(captures: &Captures, field: &str) -> Result<u32, Box<Error>> {
|
|
107
|
+ match captures.name(field) {
|
|
108
|
+ Some(capture) => Ok(capture.as_str().parse()?),
|
|
109
|
+ None => return Err(Box::new(MalformedClaim {
|
|
110
|
+ details: format!("Malformed claim line, field {} could not be found", field)
|
|
111
|
+ }))
|
|
112
|
+ }
|
|
113
|
+}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+#[cfg(test)]
|
|
117
|
+mod tests {
|
|
118
|
+ use super::*;
|
|
119
|
+
|
|
120
|
+ const TEST_INPUT: &str = "inputs/3_test.txt";
|
|
121
|
+ const TEST_INPUT_MALFORMED: &str = "inputs/3_test_malformed.txt";
|
|
122
|
+
|
|
123
|
+ #[test]
|
|
124
|
+ fn reads_claims_file() {
|
|
125
|
+ assert_eq!(read_claims(TEST_INPUT).unwrap(), vec![
|
|
126
|
+ Claim { id: 1, left: 1, top: 3, width: 4, height: 4 },
|
|
127
|
+ Claim { id: 2, left: 3, top: 1, width: 4, height: 4 },
|
|
128
|
+ Claim { id: 3, left: 5, top: 5, width: 2, height: 2 },
|
|
129
|
+ ]);
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ #[test]
|
|
133
|
+ fn errors_on_malformed_claims_file() {
|
|
134
|
+ match read_claims(TEST_INPUT_MALFORMED) {
|
|
135
|
+ Ok(_) => assert!(false, "read_claims should have returned an error"),
|
|
136
|
+ Err(err) => assert_eq!(
|
|
137
|
+ (*err).description(),
|
|
138
|
+ "Malformed claim line, no fields could be found".to_string(),
|
|
139
|
+ ),
|
|
140
|
+ }
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ #[test]
|
|
144
|
+ fn lists_points_in_claim() {
|
|
145
|
+ assert_eq!(list_points_in_claim(Claim { id: 1, left: 0, top: 0, width: 2, height: 2 }),
|
|
146
|
+ vec![
|
|
147
|
+ Point { x: 0, y: 0 },
|
|
148
|
+ Point { x: 0, y: 1 },
|
|
149
|
+ Point { x: 1, y: 0 },
|
|
150
|
+ Point { x: 1, y: 1 },
|
|
151
|
+ ]
|
|
152
|
+ )
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ #[test]
|
|
156
|
+ fn counts_overlapping_claimed_points() {
|
|
157
|
+ let test_claims = vec![
|
|
158
|
+ Claim { id: 1, left: 1, top: 3, width: 4, height: 4 },
|
|
159
|
+ Claim { id: 2, left: 3, top: 1, width: 4, height: 4 },
|
|
160
|
+ Claim { id: 3, left: 5, top: 5, width: 2, height: 2 },
|
|
161
|
+ ];
|
|
162
|
+ assert_eq!(count_overlapping_claimed_points(test_claims), 4);
|
|
163
|
+ }
|
|
164
|
+}
|