advent-of-code

advent of code (partial solutions)
Log | Files | Refs | README

commit 37adb071262339a5ebdf7a67842f6ea1c9ab8ee3
parent c22ccf9ff8e74ba860805df3fe48796b3c641461
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sun,  5 Dec 2021 23:39:56 -0800

atrocious day 5

Diffstat:
M2021/aoc05.rs | 51++++++++++++++++++++++++++++++++-------------------
1 file changed, 32 insertions(+), 19 deletions(-)

diff --git a/2021/aoc05.rs b/2021/aoc05.rs @@ -4,8 +4,8 @@ use std::io::{self, BufRead}; #[derive(Debug, Copy, Clone)] struct Point { - x: usize, - y: usize, + x: i32, + y: i32, } fn main() -> Result<(), Box<dyn Error>> { @@ -27,31 +27,44 @@ fn main() -> Result<(), Box<dyn Error>> { } }) .collect(); - pair.sort_by_key(|a| a.x); - pair.sort_by_key(|a| a.y); (pair[0], pair[1]) }) // only vertcal/horizontal - .filter(|(start, end)| start.x == end.x || start.y == end.y) .collect(); for (start, end) in segments { - for i in start.x..=end.x { - for j in start.y..=end.y { - if points[i * 1000 + j] == 0 { - points[i * 1000 + j] = - start.x * 1000000000 + start.y * 1000000 + end.x * 1000 + end.y; - } else { - println!( - "{:?}, {:?} intersects with {}", - start, - end, - points[i * 1000 + j] - ); - intersections += 1 + let irange = match start.x < end.x { + true => start.x..=end.x, + false => end.x..=start.x, + }; + + // println!("{:?}", irange); + for i in irange { + let jrange = match start.y < end.y { + true => start.y..=end.y, + false => end.y..=start.y, + }; + for j in jrange { + if start.x == end.x + || start.y == end.y + || start.x - i == j - start.y + || (start.x - i == start.y - j) + { + // println!("{:?} {:?} {:?} {:?} ", start, end, i, j); + if points[i as usize * 1000 + j as usize] == 0 { + points[i as usize * 1000 + j as usize] = 1; + } else { + // // println!( + // "{:?}, {:?} intersects {} times", + // start, + // end, + // points[i as usize * 1000 + j as usize] + // ); + points[i as usize * 1000 + j as usize] += 1; + } } } } } - println!("{:?}", intersections); + println!("{}", points.iter().filter(|p| **p > 1 as usize).count()); Ok(()) }