day 8 part 2 bruteforce attempt
This commit is contained in:
parent
5cc33ed979
commit
1c8b2b3179
1 changed files with 26 additions and 2 deletions
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use aoc23::read;
|
use aoc23::read;
|
||||||
use chumsky::prelude::*;
|
use chumsky::prelude::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
enum Instruction {
|
enum Instruction {
|
||||||
Left,
|
Left,
|
||||||
|
@ -85,7 +85,6 @@ fn parser() -> impl Parser<char, (Vec<Instruction>, Network), Error = Simple<cha
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let (instructions, network) = read("inputs/day8.txt", parser())?;
|
let (instructions, network) = read("inputs/day8.txt", parser())?;
|
||||||
// eprintln!("{network:?}");
|
|
||||||
|
|
||||||
let mut curr_node = "AAA";
|
let mut curr_node = "AAA";
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
@ -100,5 +99,30 @@ fn main() -> anyhow::Result<()> {
|
||||||
}
|
}
|
||||||
println!("{steps}");
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue