Adds day 10

This commit is contained in:
2025-02-28 18:15:13 +01:00
parent 8c116cb59a
commit 56219c16ac
4 changed files with 187 additions and 2 deletions

125
src/days/day10.rs Normal file
View File

@@ -0,0 +1,125 @@
use std::path::Path;
use advent_derive::advent_day;
use itertools::Itertools;
use itertools::iproduct;
use crate::{error::AdventError, input::read_into_vec};
use super::*;
struct Part1 {
grid: Vec<Vec<u8>>,
width: usize,
height: usize
}
impl Part1 {
fn new() -> Self {
Self {
grid: vec![],
width: 0,
height: 0
}
}
}
type Part2 = Part1;
fn read_input_part1(part1: &mut Part1, path: &Path) -> Result<(), AdventError> {
part1.grid = read_into_vec::<String>(path)?.iter().map(|s| {
s.chars().map(|c| c.to_digit(10).unwrap() as u8).collect::<Vec<u8>>()
}).collect::<Vec<Vec<u8>>>();
part1.width = part1.grid[0].len();
part1.height = part1.grid.len();
Ok(())
}
fn solve_part1(part1: &mut Part1) -> Result<String, AdventError> {
let mut trail_heads = vec![];
for y in 0..part1.height {
for x in part1.grid[y].iter().positions(|h| *h == 0) {
trail_heads.push((x, y));
}
}
let mut answer = 0;
for trail_head in trail_heads.into_iter() {
let mut trails = vec![trail_head];
for i in 0..9
{
let mut new_trails = vec![];
for (x, y) in trails.iter() {
let reachable = generate_reachable(&part1, (*x, *y));
let reachable_step = reachable.into_iter().filter(|(nx, ny)| {
part1.grid[*ny][*nx] == i+1
}).collect::<Vec<(usize, usize)>>();
for x in reachable_step.into_iter() {
new_trails.push(x);
}
}
trails = new_trails;
}
answer += trails.iter().unique().count()
}
Ok(answer.to_string())
}
fn generate_reachable(part1: &Part1, grid_point: (usize, usize)) -> Vec<(usize, usize)> {
let (x, y) = grid_point;
let mut x_coords = vec![];
let mut y_coords = vec![];
if x > 0 {
x_coords.push(x-1)
}
if x < part1.width - 1 {
x_coords.push(x+1);
}
if y > 0 {
y_coords.push(y-1)
}
if y < part1.height - 1 {
y_coords.push(y+1);
}
iproduct!(x_coords.into_iter(), vec![y].into_iter()).chain(iproduct!(vec![x].into_iter(), y_coords.into_iter())).collect::<Vec<(usize, usize)>>()
}
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> {
let mut trail_heads = vec![];
for y in 0..part2.height {
for x in part2.grid[y].iter().positions(|h| *h == 0) {
trail_heads.push((x, y));
}
}
let mut answer = 0;
for trail_head in trail_heads.into_iter() {
let mut trails = vec![trail_head];
for i in 0..9
{
let mut new_trails = vec![];
for (x, y) in trails.iter() {
let reachable = generate_reachable(&part2, (*x, *y));
let reachable_step = reachable.into_iter().filter(|(nx, ny)| {
part2.grid[*ny][*nx] == i+1
}).collect::<Vec<(usize, usize)>>();
for x in reachable_step.into_iter() {
new_trails.push(x);
}
}
trails = new_trails;
}
answer += trails.iter().count()
}
Ok(answer.to_string())
}
#[advent_day(Part1, Part2)]
struct Day10;

View File

@@ -24,7 +24,7 @@ pub mod day6;
pub mod day7;
pub mod day8;
pub mod day9;
pub mod day10;
pub mod day14;
/*

View File

@@ -13,6 +13,7 @@ use day6::Day6;
use day7::Day7;
use day8::Day8;
use day9::Day9;
use day10::Day10;
use day14::Day14;
@@ -51,7 +52,7 @@ fn main() -> Result<(), AdventError> {
Box::new(Day7::new()),
Box::new(Day8::new()),
Box::new(Day9::new()),
Box::new(DummyDay::new()),
Box::new(Day10::new()),
Box::new(DummyDay::new()),
Box::new(DummyDay::new()),
Box::new(DummyDay::new()),