Browse Source

Completed day 10 part 2

Tyler Hallada 4 years ago
parent
commit
c68eee28c7
2 changed files with 10 additions and 17 deletions
  1. 5 0
      day10/input/test6.txt
  2. 5 17
      day10/src/main.rs

+ 5 - 0
day10/input/test6.txt

1
+.#....#####...#..
2
+##...##.#####..##
3
+##...#...#.#####.
4
+..#.....X...###..
5
+..#.#.....#....##

+ 5 - 17
day10/src/main.rs

3
 use std::error::Error;
3
 use std::error::Error;
4
 use std::fs::File;
4
 use std::fs::File;
5
 use std::io::{prelude::*, BufReader};
5
 use std::io::{prelude::*, BufReader};
6
-use std::iter::FromIterator;
7
 use std::result;
6
 use std::result;
8
 
7
 
9
 use num::integer::gcd;
8
 use num::integer::gcd;
97
         let mut lines_of_sight = self.get_lines_of_sight(laser_point);
96
         let mut lines_of_sight = self.get_lines_of_sight(laser_point);
98
         let mut directions: Vec<(i32, i32)> = lines_of_sight.keys().map(|key| *key).collect();
97
         let mut directions: Vec<(i32, i32)> = lines_of_sight.keys().map(|key| *key).collect();
99
         directions.sort_by(|a, b| {
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
         let up = directions
103
         let up = directions
110
             .iter()
104
             .iter()
111
             .position(|&dir| dir == (0, -1))
105
             .position(|&dir| dir == (0, -1))
112
             .expect("No asteroid directly up from laser");
106
             .expect("No asteroid directly up from laser");
113
         directions.rotate_left(up);
107
         directions.rotate_left(up);
114
-        dbg!(&directions);
115
 
108
 
116
         for direction in directions.iter() {
109
         for direction in directions.iter() {
117
-            // dbg!(direction);
118
             let in_sight = lines_of_sight.get_mut(direction);
110
             let in_sight = lines_of_sight.get_mut(direction);
119
             if let Some(in_sight) = in_sight {
111
             if let Some(in_sight) = in_sight {
120
-                // dbg!(&in_sight);
121
                 if let Some(vaporized_asteroid) = in_sight.pop_back() {
112
                 if let Some(vaporized_asteroid) = in_sight.pop_back() {
122
                     vaporized_counter += 1;
113
                     vaporized_counter += 1;
123
 
114
 
124
-                    // dbg!(&vaporized_counter);
125
-                    // dbg!(&vaporized_asteroid);
126
-
127
                     if vaporized_counter == 200 {
115
                     if vaporized_counter == 200 {
128
                         return Some(vaporized_asteroid);
116
                         return Some(vaporized_asteroid);
129
                     }
117
                     }
157
 }
145
 }
158
 
146
 
159
 fn solve_part2() -> Result<usize> {
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
     Ok(vaporized200.x * 100 + vaporized200.y)
150
     Ok(vaporized200.x * 100 + vaporized200.y)
163
 }
151
 }
164
 
152