-
Notifications
You must be signed in to change notification settings - Fork 1
/
a.rs
87 lines (73 loc) · 2.13 KB
/
a.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
use atoi::atoi;
use nom::*;
pub fn run() -> usize {
let input = include_str!("./input.txt");
return solve(input, 1000)
}
fn solve(input: &str, dimension: usize) -> usize {
let (mut grid, mut overlaps) = (vec![0u8; 1000 * 1000], 0);
// Define a function for plotting a point on the grid and incrementing overlap count
let mut plot = |x: usize, y: usize| {
let pos = x + y*dimension;
if grid[pos] == 1 {
overlaps += 1;
}
grid[pos] += 1;
};
// Parse as bytes to allow nom library to chomp away
input
.as_bytes()
.split(|c| *c == b'\n' )
.map(|l| {
let ((x1,y1), (x2,y2)) = line(l).unwrap().1;
// Simplify calculations by ordering all points to ensure steps are always positive.
// We are ignoring diagonals here
return if x1 == x2 && y1 > y2 {
((x1, y2), (x2, y1))
} else if y1 == y2 && x1 > x2 {
((x2, y1), (x1, y2))
} else {
((x1, y1), (x2, y2))
}
})
.for_each(|((x1, y1), (x2, y2))| {
if x1 == x2 {
(y1..=y2).for_each(|py| plot(x1, py));
} else if y1 == y2 {
(x1..=x2).for_each(|px| plot(px, y1));
}
});
return overlaps;
}
// This is pretty amazing, love learning rust, https://docs.rs/nom/6.1.2/nom/macro.named.html
named!(coord<&[u8], usize>, map_opt!(nom::character::complete::digit1, atoi));
named!(coords<&[u8], (usize, usize)>, separated_pair!(coord, char!(','), coord));
named!(line<&[u8], ((usize, usize), (usize, usize))>, separated_pair!(coords, tag!(" -> "), coords));
#[cfg(test)]
mod tests {
extern crate test;
use test::Bencher;
use super::*;
#[test]
fn test_example() {
let input = r"0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2";
assert_eq!(solve(input, 10), 5);
}
#[test]
fn test_puzzle() {
assert_eq!(run(), 5169);
}
#[bench]
fn bench_run(b: &mut Bencher) {
b.iter(|| run())
}
}