From 1c8b2b3179dfc4b2f2b353e3df1f3d6983ec4a2a Mon Sep 17 00:00:00 2001 From: Dominic Date: Fri, 8 Dec 2023 21:28:36 +0100 Subject: [PATCH] day 8 part 2 bruteforce attempt --- src/bin/day8.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/bin/day8.rs b/src/bin/day8.rs index 1049b88..5423e2c 100644 --- a/src/bin/day8.rs +++ b/src/bin/day8.rs @@ -2,7 +2,7 @@ use aoc23::read; use chumsky::prelude::*; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; enum Instruction { Left, @@ -85,7 +85,6 @@ fn parser() -> impl Parser, Network), Error = Simple anyhow::Result<()> { let (instructions, network) = read("inputs/day8.txt", parser())?; - // eprintln!("{network:?}"); let mut curr_node = "AAA"; let mut i = 0; @@ -100,5 +99,30 @@ fn main() -> anyhow::Result<()> { } println!("{steps}"); + let mut curr_nodes: HashSet<&str> = network + .nodes + .keys() + .map(|node| node.as_str()) + .filter(|node| node.ends_with('A')) + .collect(); + dbg!(curr_nodes.len()); + let mut i = 0; + let mut steps = 0; + while curr_nodes.iter().any(|node| !node.ends_with('Z')) { + curr_nodes = curr_nodes + .into_iter() + .map(|node| match instructions[i] { + Instruction::Left => network.left(node), + Instruction::Right => network.right(node) + }) + .collect(); + i = (i + 1) % instructions.len(); + steps += 1; + if steps % 10000 == 0 { + dbg!(steps); + } + } + println!("{steps}"); + Ok(()) }