Guest User

Untitled

a guest
Dec 5th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.52 KB | None | 0 0
  1. use nalgebra::DMatrix;
  2.  
  3. macro_rules! parse {
  4.     ($x: ident, $y: ident) => {
  5.         {
  6.             let x = $x.parse();
  7.             let y = $y.parse();
  8.  
  9.             if x.is_err() || y.is_err() {
  10.                 return None;
  11.             }
  12.  
  13.             (x.unwrap_or(0), y.unwrap_or(0))
  14.         }
  15.     };
  16. }
  17.  
  18. macro_rules! max {
  19.     ($x: expr) => ($x);
  20.     ($x: expr, $($z: expr),+) => (::std::cmp::max($x, max!($($z),*)));
  21. }
  22.  
  23. #[derive(Debug)]
  24. struct Point {
  25.     x: i32,
  26.     y: i32,
  27. }
  28.  
  29. fn aoc_05(points: &[(Point, Point)], max_x: i32, max_y: i32, only_vertical_horizontal: bool) -> u32 {
  30.     let mut matrix = DMatrix::<i32>::zeros(max_x.abs() as usize + 1, max_y.abs() as usize + 1);
  31.  
  32.     for (p1, p2) in points {
  33.         let dx = (p2.x - p1.x).signum();
  34.         let dy = (p2.y - p1.y).signum();
  35.         let (mut x, mut y) = (p1.x, p1.y);
  36.  
  37.         // Part 1...
  38.         if only_vertical_horizontal && dx != 0 && dy != 0 {
  39.             continue;
  40.         }
  41.  
  42.         while (x, y) != (p2.x + dx, p2.y + dy) {
  43.             matrix[(x as usize, y as usize)] += 1;
  44.             x += dx;
  45.             y += dy;
  46.         }
  47.     }
  48.  
  49.     matrix.fold(0, |mut iter, val| {
  50.         if val > 1 {
  51.             iter += 1;
  52.         }
  53.         iter
  54.     })
  55. }
  56.  
  57. fn parse_input(s: &str) -> Option<(Point, Point)> {
  58.     match s.split_once(" -> ") {
  59.         Some((p1, p2)) => {
  60.             match p1.split_once(',') {
  61.                 Some((x1, y1)) => {
  62.                     let (x1, y1) = parse!(x1, y1);
  63.  
  64.                     match p2.split_once(',') {
  65.                         Some((x2, y2)) => {
  66.                             let (x2, y2) = parse!(x2, y2);
  67.                             Some((Point { x: x1, y: y1 }, Point { x: x2, y: y2 }))
  68.                         },
  69.                         None => None
  70.                     }
  71.                 },
  72.                 None => None
  73.             }
  74.         },
  75.         None => None
  76.     }
  77. }
  78.  
  79. fn main() {
  80.     // Get the data
  81.     let points: Vec<(Point, Point)> = include_str!("../input.txt").lines().filter_map(parse_input).collect::<Vec<(Point, Point)>>();
  82.  
  83.     // Get the max of x and y
  84.     let (max_x, max_y) = points.iter().fold((0, 0), |max, val| (max!(val.0.x, val.1.x, max.0), max!(val.0.y, val.1.y, max.1)));
  85.  
  86.     // Current matrix settings
  87.     println!("Max x {} max y {}", max_x, max_y);
  88.     let result = aoc_05(&points, max_x, max_y, true);
  89.     println!("Total #1 {}", result);
  90.     // Same code...
  91.     let result = aoc_05(&points, max_x, max_y, false);
  92.     println!("Total #2 {}", result);
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment