Skip to content

Commit

Permalink
[2023] Day 21: Part b insights
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 21, 2023
1 parent 7b2a72b commit 35a92a4
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions aoc_2023/src/day_21.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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::<i32>().unwrap();
let size = map.size.num_cast::<i32>().unwrap();
let (sx, sy) = (size.x(), size.y());

let mut pos = HashSet::new();
pos.insert(map.find(Tile::Start).unwrap().num_cast::<i32>().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::<usize>().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);
}
}
Expand All @@ -86,6 +73,13 @@ impl Solution for Day21 {
}
}

fn map_pos(pos: Vec2<i32>, size: Vec2<i32>) -> Vec2<usize> {
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,
Expand Down

0 comments on commit 35a92a4

Please sign in to comment.