-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay05.rs
63 lines (53 loc) · 1.48 KB
/
Day05.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
use std::collections::HashMap;
use std::env;
use std::io;
fn main() {
let nums: Vec<Vec<u32>> = io::stdin()
.lines()
.map(|s| s.unwrap())
.map(|s| {
s.split(" -> ")
.flat_map(|s| s.split(","))
.map(str::parse)
.map(Result::unwrap)
.collect()
})
.collect();
let step = |a: u32, b: u32| {
if a < b {
a + 1
} else if a > b {
a - 1
} else {
a
}
};
let criteria: fn(&&Vec<u32>) -> bool = if env::args().nth(1).unwrap() == "1" {
// Part one, use only horizontal and vertical
|&v: &&Vec<u32>| match v[..] {
[x1, y1, x2, y2] => x1 == x2 || y1 == y2,
_ => false,
}
} else {
// Part two, use all
|_| true
};
let mut board: HashMap<(u32, u32), u32> = HashMap::new();
for v in nums.iter().filter(criteria) {
match v[..] {
[x1, y1, x2, y2] => {
let mut x = x1;
let mut y = y1;
while !(x == x2 && y == y2) {
*board.entry((y, x)).or_insert(0) += 1;
x = step(x, x2);
y = step(y, y2);
}
*board.entry((y, x)).or_insert(0) += 1;
}
_ => (),
}
}
let count = board.iter().map(|kv| kv.1).filter(|&&v| v > 1).count();
println!("{}", count);
}