:)
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

day9 part2 complete

+38 -37
+6 -8
src/day9.rs
··· 17 17 pub fn day9_part2(input: &str) -> String { 18 18 let points = parse(input); 19 19 20 - // let red_or_green_tiles_shape = RightAngledShape::new(&points); 20 + let red_or_green_tiles_shape = RightAngledShape::new(&points); 21 21 22 22 let pair = points 23 - .iter() 23 + .into_iter() 24 24 .combinations(2) 25 25 .map(|pair| (pair[0], pair[1])) 26 - .filter(|(l, r)| { 27 - !points 28 - .iter() 29 - .any(|pt| Point::rect_strict_contains_point(**l, **r, *pt)) 26 + .filter(|rect| { 27 + red_or_green_tiles_shape.contains_rect(*rect) 30 28 }) 31 29 .max_by_key(|(l, r)| l.rectangular_area(r)) 32 30 .unwrap(); 33 31 34 - dbg!(pair); 35 - pair.0.rectangular_area(pair.1).to_string() 32 + dbg!(&pair); 33 + pair.0.rectangular_area(&pair.1).to_string() 36 34 } 37 35 38 36 fn parse(input: &str) -> Vec<Point> {
+2 -2
src/lib.rs
··· 140 140 fn day9_part2_test() { 141 141 let test_result = day9::day9_part2(include_str!("../input/day9.test.txt")); 142 142 assert_eq!(test_result, "24"); 143 - // let result = day9::day9_part2(include_str!("../input/day9.txt")); 144 - // assert_eq!(result, "6083499488"); 143 + let result = day9::day9_part2(include_str!("../input/day9.txt")); 144 + assert_eq!(result, "1343576598"); 145 145 } 146 146 147 147
+30 -27
src/spatial.rs
··· 1 - use std::{collections::BTreeSet, convert::Infallible, num::ParseIntError, str::FromStr}; 1 + use std::{num::ParseIntError, str::FromStr}; 2 2 3 3 use itertools::Itertools; 4 4 ··· 25 25 let width = self.col.abs_diff(r.col) + 1; 26 26 width * height 27 27 } 28 - pub fn rect_strict_contains_point(r1: Point, r2: Point, p: Point) -> bool { 29 - let row_condition = if r1.row <= r2.row { 30 - p.row > r1.row && p.row < r2.row 31 - } else { 32 - p.row < r1.row && p.row > r2.row 33 - }; 34 - let col_condition = if r1.col <= r2.col { 35 - p.col > r1.col && p.col < r2.col 36 - } else { 37 - p.col < r1.col && p.col > r2.col 38 - }; 39 - dbg!(r1, r2, p, row_condition, col_condition); 40 - 41 - row_condition && col_condition 42 - } 43 28 } 44 29 45 30 #[derive(Debug, Clone)] ··· 54 39 } 55 40 } 56 41 57 - pub fn area(&self) -> usize { 58 - todo!() 42 + pub fn contains_rect(&self, rect: (Point, Point)) -> bool { 43 + !(Self::rect_strict_contains_line( 44 + rect, 45 + (self.corners[0], self.corners[self.corners.len() - 1]), 46 + ) || self 47 + .corners 48 + .windows(2) 49 + .any(|window| Self::rect_strict_contains_line(rect, (window[0], window[1])))) 59 50 } 60 51 61 - pub fn contains_rect(&self, other_corner1: Point, other_corner2: Point) -> bool { 62 - todo!() 52 + fn rect_strict_contains_line(rect: (Point, Point), line: (Point, Point)) -> bool { 53 + let left_col = rect.0.col.min(rect.1.col); 54 + let right_col = rect.0.col.max(rect.1.col); 55 + let top_row = rect.0.row.min(rect.1.row); 56 + let bottom_row = rect.0.row.max(rect.1.row); 57 + 58 + if line.0.row == line.1.row { 59 + //horizontal line 60 + let row_cond = line.0.row >= bottom_row || line.0.row <= top_row; 61 + let left_cond = line.0.col <= left_col && line.1.col <= left_col; 62 + let right_cond = line.0.col >= right_col && line.1.col >= right_col; 63 + 64 + !row_cond && !left_cond && !right_cond 65 + } else { 66 + //vertical line 67 + let col_cond = line.0.col >= right_col || line.0.col <= left_col; 68 + let above_cond = line.0.row <= top_row && line.1.row <= top_row; 69 + let below_cond = line.0.row >= bottom_row && line.1.row >= bottom_row; 70 + 71 + !col_cond && !above_cond && !below_cond 72 + } 63 73 } 64 74 } 65 75 ··· 77 87 + (self.z as isize - other.z as isize).abs().pow(2)) as f64) 78 88 .sqrt() 79 89 } 80 - pub fn format(&self) -> String { 81 - format!("{},{},{}", self.x, self.y, self.z) 82 - } 83 - } 84 - 85 - fn connected_components(pairs: &[(Point3D, Point3D)]) -> BTreeSet<BTreeSet<Point>> { 86 - todo!() 87 90 } 88 91 89 92 pub fn all_coords(width: usize, height: usize) -> impl Iterator<Item = Point> {