diff --git a/src/day8.rs b/src/day8.rs new file mode 100644 index 0000000..c01bd3e --- /dev/null +++ b/src/day8.rs @@ -0,0 +1,64 @@ +use std::collections::{HashMap, HashSet}; + +use itertools::Itertools; + +use crate::get_lines; + +type Cords = (isize, isize); + +#[allow(clippy::cast_possible_wrap)] +fn parse_field() -> (Cords, HashMap>) { + let mut positions: HashMap> = HashMap::new(); + let mut dimentions = (0, 0); + for (i, line) in get_lines("input.txt").enumerate() { + dimentions = (dimentions.0 + 1, dimentions.1.max(line.len() as isize)); + for (j, c) in line.chars().enumerate() { + if c == '.' || c == '#' { + continue; + } + positions + .entry(c) + .or_default() + .push((i as isize, j as isize)); + } + } + (dimentions, positions) +} + +#[allow(dead_code)] +pub fn task1() { + let (dimentions, positions) = parse_field(); + let mut affected: HashSet = HashSet::new(); + for pos in positions.into_values() { + for vals in pos.iter().copied().permutations(2) { + let [(y1, x1), (y2, x2)] = *vals.as_slice() else { + unreachable!() + }; + let (x3, y3) = (x2 * 2 - x1, y2 * 2 - y1); + if (0..dimentions.0).contains(&y3) && (0..dimentions.1).contains(&x3) { + affected.insert((y3, x3)); + } + } + } + println!("{}", affected.len()); +} + +#[allow(dead_code)] +pub fn task2() { + let (dimentions, positions) = parse_field(); + let mut affected: HashSet = HashSet::new(); + for pos in positions.into_values() { + for vals in pos.iter().copied().permutations(2) { + let [(y1, x1), (mut y2, mut x2)] = *vals.as_slice() else { + unreachable!() + }; + let (dx, dy) = (x2 - x1, y2 - y1); + while (0..dimentions.0).contains(&y2) && (0..dimentions.1).contains(&x2) { + affected.insert((y2, x2)); + x2 += dx; + y2 += dy; + } + } + } + println!("{}", affected.len()); +} diff --git a/src/main.rs b/src/main.rs index 0e6ea05..3df0c8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod day4; mod day5; mod day6; mod day7; +mod day8; use std::{ fs::File, @@ -22,6 +23,6 @@ fn parse_line(line: &str, pattern: char) -> impl Iterator + '_ { } fn main() { - day7::task1(); - day7::task2(); + day8::task1(); + day8::task2(); }