-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.rs
135 lines (123 loc) · 2.94 KB
/
10.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
use std::collections::BTreeSet;
static INPUT_TXT: &str = include_str!("../../input/10.txt");
fn main() {
println!("Part 1: {}", part_1(INPUT_TXT));
println!("Part 2: {}", part_2(INPUT_TXT));
}
#[must_use]
pub fn part_1(input: &str) -> usize {
let asteroids: Vec<_> = input
.trim()
.lines()
.enumerate()
.flat_map(|(y, line)| {
line.trim().chars().enumerate().filter_map(move |(x, ch)| {
if ch == '#' {
Some((x as i8, y as i8))
} else {
None
}
})
})
.collect();
asteroids
.iter()
.map(|&from| {
asteroids
.iter()
.filter_map(|&to| {
if from != to {
let relative = (from.0 - to.0, from.1 - to.1);
Some(
(f64::from(relative.1)
.atan2(f64::from(relative.0))
.to_degrees()
* 10.0) as i16,
)
} else {
None
}
})
.collect::<BTreeSet<i16>>()
.len()
})
.max()
.unwrap()
}
#[must_use]
pub fn part_2(_input: &str) -> i64 {
0
}
#[cfg(test)]
mod tests {
use super::*;
static INPUT_1: &str = ".#..#
.....
#####
....#
...##";
static INPUT_2: &str = "......#.#.
#..#.#....
..#######.
.#.#.###..
.#..#.....
..#....#.#
#..#....#.
.##.#..###
##...#..#.
.#....####";
static INPUT_3: &str = "#.#...#.#.
.###....#.
.#....#...
##.#.#.#.#
....#.#.#.
.##..###.#
..#...##..
..##....##
......#...
.####.###.";
static INPUT_4: &str = ".#..#..###
####.###.#
....###.#.
..###.##.#
##.##.#.#.
....###..#
..#.#..#.#
#..#.#.###
.##...##.#
.....#.#..";
static INPUT_5: &str = ".#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##";
#[test]
fn test_part_1() {
assert_eq!(part_1(INPUT_1), 8);
assert_eq!(part_1(INPUT_2), 33);
assert_eq!(part_1(INPUT_3), 35);
assert_eq!(part_1(INPUT_4), 41);
assert_eq!(part_1(INPUT_5), 210);
assert_eq!(part_1(INPUT_TXT), 230);
}
#[test]
fn test_part_2() {
assert_eq!(part_2(INPUT_1), 0);
assert_eq!(part_2(INPUT_TXT), 0);
}
}