From 35a92a4b9f77bc6ecf6c4f39c7e54a95cd7c6d37 Mon Sep 17 00:00:00 2001 From: Connor Slade Date: Thu, 21 Dec 2023 02:15:29 -0500 Subject: [PATCH] [2023] Day 21: Part b insights --- aoc_2023/src/day_21.rs | 44 ++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/aoc_2023/src/day_21.rs b/aoc_2023/src/day_21.rs index 7a1e174..9a8ff57 100644 --- a/aoc_2023/src/day_21.rs +++ b/aoc_2023/src/day_21.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use common::{Answer, Solution}; -use nd_vec::vector; +use nd_vec::{vector, Vec2}; use crate::aoc_lib::{direction::Direction, matrix::Matrix}; @@ -39,39 +39,26 @@ impl Solution for Day21 { fn part_b(&self, input: &str) -> Answer { let map = parse(input); - + let start = map.find(Tile::Start).unwrap().num_cast::().unwrap(); let size = map.size.num_cast::().unwrap(); - let (sx, sy) = (size.x(), size.y()); let mut pos = HashSet::new(); - pos.insert(map.find(Tile::Start).unwrap().num_cast::().unwrap()); - - for i in 0..500 { - println!("{:.1}% done", i as f32 / 500.0 * 100.0); + pos.insert(start); + for i in 0..1000 { let mut new_pos = HashSet::new(); + if i % size.x() == 65 { + println!("({i}, {})", pos.len()); + // Pipe the first few values into wolfram alpha to get a formula then evaluate it with `26501365` + // Ex: curve fit (65, 3797), (196, 34009), (327, 94353) + } + for p in pos { for dir in Direction::ALL { let new_p = dir.advance(p); - if { - let mut pos = new_p; - - if pos.x() < 0 { - pos = vector!(sx + pos.x() % sx, pos.y()); - } - - if pos.y() < 0 { - pos = vector!(pos.x(), sy + pos.y() % sy); - } - - pos = vector!(pos.x() % sx, pos.y()); - pos = vector!(pos.x(), pos.y() % sy); - - let pos = pos.num_cast::().unwrap(); - *map.get(pos).unwrap() - } != Tile::Wall - { + let mapped = map_pos(new_p, size); + if *map.get(mapped).unwrap() != Tile::Wall { new_pos.insert(new_p); } } @@ -86,6 +73,13 @@ impl Solution for Day21 { } } +fn map_pos(pos: Vec2, size: Vec2) -> Vec2 { + let mut mapped = pos; + mapped = vector!((size.x() + mapped.x() % size.x()) % size.x(), mapped.y()); + mapped = vector!(mapped.x(), (size.y() + mapped.y() % size.y()) % size.y()); + mapped.num_cast().unwrap() +} + #[derive(Debug, PartialEq, Eq, Copy, Clone)] enum Tile { Garden,