advent-of-code

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

commit c22ccf9ff8e74ba860805df3fe48796b3c641461
parent 40a4f09685c6687dfa5a8e295d5eeb04e15160cb
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  4 Dec 2021 23:31:36 -0800

broken day 5??

Diffstat:
A2021/aoc05.rs | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+), 0 deletions(-)

diff --git a/2021/aoc05.rs b/2021/aoc05.rs @@ -0,0 +1,57 @@ +use std::collections::HashSet; +use std::error::Error; +use std::io::{self, BufRead}; + +#[derive(Debug, Copy, Clone)] +struct Point { + x: usize, + y: usize, +} + +fn main() -> Result<(), Box<dyn Error>> { + let stdin = io::stdin(); + let mut points: [usize; 1000000] = [0; 1000000]; + let mut intersections = 0; + let segments: Vec<(Point, Point)> = stdin + .lock() + .lines() + .map(|l| { + let mut pair: Vec<Point> = l + .unwrap() + .split(" -> ") + .map(|p| { + let s: Vec<&str> = p.split(",").collect(); + Point { + x: s[0].parse().unwrap(), + y: s[1].parse().unwrap(), + } + }) + .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 + } + } + } + } + println!("{:?}", intersections); + Ok(()) +}