generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.rs
156 lines (145 loc) · 4.59 KB
/
03.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::collections::HashMap;
advent_of_code::solution!(3);
pub fn part_one(input: &str) -> Option<u32> {
let (partlist, linklist) = parse_input(input);
let res = partlist
.iter()
.filter_map(|(_pos, part)| {
if part
.neighbours()
.iter()
.any(|pos| linklist.contains_key(pos))
{
return Some(part.val);
}
None
})
.sum::<i32>();
Some(res as u32)
}
pub fn part_two(input: &str) -> Option<u32> {
let (partlist, mut linklist) = parse_input(input);
for (pos, part) in partlist.iter() {
for neighbour in part.neighbours() {
if let Some(link) = linklist.get_mut(&neighbour) {
link.neighbours.push(*pos);
}
}
}
let res = linklist
.iter()
.filter(|(_pos, link)| link.val == '*' && link.neighbours.len() == 2)
.map(|(_pos, link)| {
partlist.get(&link.neighbours[0]).unwrap().val
* partlist.get(&link.neighbours[1]).unwrap().val
})
.sum::<i32>();
Some(res as u32)
}
#[derive(Debug, Clone, Copy)]
struct Part {
val: i32,
x: i32,
y: i32,
width: i32,
}
#[derive(Debug, Clone)]
struct Link {
val: char,
neighbours: Vec<(i32, i32)>,
}
impl Part {
fn neighbours(&self) -> Vec<(i32, i32)> {
let mut neighbours = Vec::new();
for x in (self.x - 1)..=self.x + self.width {
for y in vec![self.y - 1, self.y + 1] {
neighbours.push((x, y));
}
}
neighbours.push((self.x - 1, self.y));
neighbours.push((self.x + self.width, self.y));
neighbours
}
}
fn parse_input(input: &str) -> (HashMap<(i32, i32), Part>, HashMap<(i32, i32), Link>) {
let mut partlist: HashMap<(i32, i32), Part> = HashMap::new();
let mut linklist: HashMap<(i32, i32), Link> = HashMap::new();
input.lines().enumerate().for_each(|(y, line)| {
let mut active_part = Option::<Part>::None;
line.chars().enumerate().for_each(|(x, c)| {
if c.is_numeric() {
if let Some(ref mut part) = active_part {
// Extend the current part
part.val = part.val * 10 + c.to_digit(10).unwrap() as i32;
part.width += 1;
} else {
// Start a new part
active_part = Some(Part {
val: c.to_digit(10).unwrap() as i32,
x: x as i32,
y: y as i32,
width: 1,
});
}
} else {
if let Some(part) = active_part {
// End the current part
partlist.insert((part.x, part.y), part);
active_part = None;
}
if c != '.' {
// Add the current character
linklist.insert(
(x as i32, y as i32),
Link {
val: c,
neighbours: vec![],
},
);
}
}
});
// End the current part (end of line edge case)
if let Some(part) = active_part {
partlist.insert((part.x, part.y), part);
}
});
(partlist, linklist)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(4361));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(467835));
}
#[test]
fn test_neighbours() {
let part = Part {
val: 1,
x: 1,
y: 1,
width: 3,
};
let neighbours = part.neighbours();
assert!(neighbours.contains(&(0, 0)));
assert!(neighbours.contains(&(1, 0)));
assert!(neighbours.contains(&(2, 0)));
assert!(neighbours.contains(&(3, 0)));
assert!(neighbours.contains(&(4, 0)));
assert!(neighbours.contains(&(0, 1)));
assert!(neighbours.contains(&(4, 1)));
assert!(neighbours.contains(&(0, 2)));
assert!(neighbours.contains(&(1, 2)));
assert!(neighbours.contains(&(2, 2)));
assert!(neighbours.contains(&(3, 2)));
assert!(neighbours.contains(&(4, 2)));
assert_eq!(neighbours.len(), 12);
}
}