This commit is contained in:
StNicolay 2024-12-08 17:58:28 +03:00
parent 8a61db424b
commit 08697c2dc1
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
2 changed files with 67 additions and 2 deletions

64
src/day8.rs Normal file
View File

@ -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<char, Vec<Cords>>) {
let mut positions: HashMap<char, Vec<Cords>> = 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<Cords> = 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 {
panic!()
};
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<Cords> = 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 {
panic!()
};
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());
}

View File

@ -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<Item = i32> + '_ {
}
fn main() {
day7::task1();
day7::task2();
day8::task1();
day8::task2();
}