Adds day8

This commit is contained in:
2025-02-20 21:27:22 +01:00
parent 311cc0509a
commit bc0aa70ac3
6 changed files with 230 additions and 19 deletions

126
src/days/day8.rs Normal file
View File

@@ -0,0 +1,126 @@
use std::{collections::HashMap, path::Path};
use advent_derive::advent_day;
use itertools::Itertools;
use gcd::Gcd;
use super::*;
use crate::{error::AdventError, input::get_lines};
struct Part1{
antennas: HashMap<char, Vec<Coord>>,
width: i32,
height: i32,
}
type Part2 = Part1;
type Coord = (i32, i32);
impl Part1 {
fn new() -> Self {
Self{
antennas: HashMap::<char, Vec<Coord>>::new(),
width: 0,
height: 0
}
}
}
impl Part2 {
}
fn read_input_part1(part1: &mut Part1, path: &Path) -> Result<(), AdventError> {
for (y, line) in get_lines(path).enumerate() {
for (x, c) in line?.chars().enumerate() {
if c != '.' {
if part1.antennas.contains_key(&c) {
part1.antennas.get_mut(&c).expect("This should never happen").push((x as i32, y as i32));
}
else {
part1.antennas.insert(c, vec![(x as i32, y as i32)]);
}
}
part1.width = (x + 1) as i32;
}
part1.height = (y + 1) as i32;
}
Ok(())
}
fn add(left: &Coord, right: &Coord) -> Coord {
(left.0 + right.0, left.1 + right.1)
}
fn sub(left: &Coord, right: &Coord) -> Coord {
(left.0 - right.0, left.1 - right.1)
}
fn gen_anti_nodes(left: &Coord, right: &Coord) -> Vec<Coord> {
let diff = sub(left, right);
let mut antis = vec![sub(right, &diff), add(left, &diff)];
if diff.0.rem_euclid(3) == 0 && diff.1.rem_euclid(3) == 0 {
antis.push((left.0 - diff.0 / 3, left.1 - diff.1 / 3));
antis.push((left.0 - diff.0 / 3 * 2, left.1 - diff.1 / 3 * 2));
}
antis
}
fn gen_anti_nodes2(left: &Coord, right: &Coord, part2: &Part2) -> Vec<Coord> {
let diff = sub(left, right);
let gcd = diff.0.unsigned_abs().gcd(diff.1.unsigned_abs());
let node_distance = (diff.0 / (gcd as i32), diff.1 / (gcd as i32));
let mut antis = vec![*left];
for i in 1.. {
let d = (node_distance.0 * i, node_distance.1 * i);
let n1 = sub(left, &d);
let n2 = add(left, &d);
let b1 = in_bounds(&n1, part2);
let b2 = in_bounds(&n2, part2);
if b1 {
antis.push(n1);
}
if b2 {
antis.push(n2);
}
if !b1 && !b2 {
break;
}
}
antis
}
fn in_bounds(coord: &Coord, part1: &Part1) -> bool {
coord.0 >= 0 && coord.0 < part1.width && coord.1 >= 0 && coord.1 < part1.height
}
fn solve_part1(part1: &mut Part1) -> Result<String, AdventError> {
Ok(part1.antennas.iter().map(|(_, coords)| {
coords.iter().enumerate().map(|(i, coord1)| {
coords[i+1..].iter().map(|coord2| {
let antis = gen_anti_nodes(coord1, coord2);
antis
})
}).flatten().flatten().filter(|coord| in_bounds(coord, part1))
}).flatten().unique().count().to_string())
}
fn read_input_part2(part2: &mut Part2, path: &Path) -> Result<(), AdventError> {
read_input_part1(part2, path)
}
fn solve_part2(part2: &mut Part2) -> Result<String, AdventError> {
Ok(part2.antennas.iter().map(|(_, coords)| {
coords.iter().enumerate().map(|(i, coord1)| {
coords[i+1..].iter().map(|coord2| {
let antis = gen_anti_nodes2(coord1, coord2, part2);
antis
})
}).flatten().flatten()
}).flatten().collect::<Vec<Coord>>().iter().unique().count().to_string())
}
#[advent_day(Part1, Part2)]
struct Day8;

View File

@@ -22,23 +22,48 @@ pub mod day4;
pub mod day5;
pub mod day6;
pub mod day7;
pub mod day8;
pub mod day14;
/*
fn read_input_part1(part1: &mut Day5Part1, path: &Path) -> Result<(), AdventError> {
use std::path::Path;
use crate::error::AdventError;
struct Part1{}
struct Part2{}
impl Part1 {
fn new() -> Self {
Self{}
}
}
impl Part2 {
fn new() -> Self{
Self{}
}
}
fn read_input_part1(part1: &mut Part1, path: &Path) -> Result<(), AdventError> {
Ok(())
}
fn solve_part1(part1: &mut Day5Part1) -> Result<u64, AdventError> {
Ok(0)
fn solve_part1(part1: &mut Part1) -> Result<String, AdventError> {
Ok("0".to_string())
}
fn read_input_part2(part2: &mut Day5Part2, path: &Path) -> Result<(), AdventError> {
fn read_input_part2(part2: &mut Part2, path: &Path) -> Result<(), AdventError> {
Ok(())
}
fn solve_part2(part2: &mut Day5Part2) -> Result<u64, AdventError> {
Ok(0)
fn solve_part2(part2: &mut Part2) -> Result<String, AdventError> {
Ok("0".to_string())
}
#[advent_day(Part1, Part2)]
struct Day8;
*/

View File

@@ -11,10 +11,12 @@ use day4::Day4;
use day5::Day5;
use day6::Day6;
use day7::Day7;
use day8::Day8;
use day14::Day14;
use crate::error::AdventError;
use crate::days::*;
pub use crate::days::*;
struct DummyDay {
@@ -46,7 +48,7 @@ fn main() -> Result<(), AdventError> {
Box::new(Day5::new()),
Box::new(Day6::new()),
Box::new(Day7::new()),
Box::new(DummyDay::new()),
Box::new(Day8::new()),
Box::new(DummyDay::new()),
Box::new(DummyDay::new()),
Box::new(DummyDay::new()),