|
@@ -3,7 +3,6 @@ use std::collections::{HashMap, VecDeque};
|
3
|
3
|
use std::error::Error;
|
4
|
4
|
use std::fs::File;
|
5
|
5
|
use std::io::{prelude::*, BufReader};
|
6
|
|
-use std::iter::FromIterator;
|
7
|
6
|
use std::result;
|
8
|
7
|
|
9
|
8
|
use num::integer::gcd;
|
|
@@ -97,33 +96,22 @@ impl AsteroidField {
|
97
|
96
|
let mut lines_of_sight = self.get_lines_of_sight(laser_point);
|
98
|
97
|
let mut directions: Vec<(i32, i32)> = lines_of_sight.keys().map(|key| *key).collect();
|
99
|
98
|
directions.sort_by(|a, b| {
|
100
|
|
- let det = a.0 * b.1 - b.0 * a.1;
|
101
|
|
- if det > 0 {
|
102
|
|
- Ordering::Less
|
103
|
|
- } else if det < 0 {
|
104
|
|
- Ordering::Greater
|
105
|
|
- } else {
|
106
|
|
- Ordering::Equal
|
107
|
|
- }
|
|
99
|
+ let a_deg = (a.1 as f32).atan2(a.0 as f32);
|
|
100
|
+ let b_deg = (b.1 as f32).atan2(b.0 as f32);
|
|
101
|
+ a_deg.partial_cmp(&b_deg).unwrap_or(Ordering::Equal)
|
108
|
102
|
});
|
109
|
103
|
let up = directions
|
110
|
104
|
.iter()
|
111
|
105
|
.position(|&dir| dir == (0, -1))
|
112
|
106
|
.expect("No asteroid directly up from laser");
|
113
|
107
|
directions.rotate_left(up);
|
114
|
|
- dbg!(&directions);
|
115
|
108
|
|
116
|
109
|
for direction in directions.iter() {
|
117
|
|
- // dbg!(direction);
|
118
|
110
|
let in_sight = lines_of_sight.get_mut(direction);
|
119
|
111
|
if let Some(in_sight) = in_sight {
|
120
|
|
- // dbg!(&in_sight);
|
121
|
112
|
if let Some(vaporized_asteroid) = in_sight.pop_back() {
|
122
|
113
|
vaporized_counter += 1;
|
123
|
114
|
|
124
|
|
- // dbg!(&vaporized_counter);
|
125
|
|
- // dbg!(&vaporized_asteroid);
|
126
|
|
-
|
127
|
115
|
if vaporized_counter == 200 {
|
128
|
116
|
return Some(vaporized_asteroid);
|
129
|
117
|
}
|
|
@@ -157,8 +145,8 @@ fn solve_part1() -> Result<usize> {
|
157
|
145
|
}
|
158
|
146
|
|
159
|
147
|
fn solve_part2() -> Result<usize> {
|
160
|
|
- let mut asteroid_field = read_asteroid_field("input/test5.txt")?;
|
161
|
|
- let vaporized200 = asteroid_field.vaporize_asteroids(&Point { x: 11, y: 13 }).unwrap();
|
|
148
|
+ let mut asteroid_field = read_asteroid_field(INPUT)?;
|
|
149
|
+ let vaporized200 = asteroid_field.vaporize_asteroids(&Point { x: 22, y: 25 }).unwrap();
|
162
|
150
|
Ok(vaporized200.x * 100 + vaporized200.y)
|
163
|
151
|
}
|
164
|
152
|
|