From f6e4d9cea0279032100e42cd9e71877a8460184e Mon Sep 17 00:00:00 2001 From: timeshifter Date: Thu, 25 Jun 2026 22:22:33 +0200 Subject: [PATCH] cant figure 19 out --- day19/src/main.rs | 59 ++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/day19/src/main.rs b/day19/src/main.rs index 3d1376b..ccf5eae 100644 --- a/day19/src/main.rs +++ b/day19/src/main.rs @@ -36,42 +36,41 @@ fn main() { part1(conversions, &separate); // 576 - main2(); + _part2(conversions, formula); + // main2(); } -// fn _part2(conversions: &[(&str, &str)], formula: &str) { -// let mut rng = rand::rng(); +fn _part2(conversions: &[(&str, &str)], formula: &str) { + let mut rng = rand::rng(); -// let mut rand_conversions = conversions.to_vec(); -// let mut old_conversion_count = 10000; // large -// let mut conversion_count; + let mut rand_conversions = conversions.to_vec(); + // let mut old_conversion_count = 10000; // large + let mut conversion_count; -// loop { -// conversion_count = 0; -// let mut formula_replaced = formula.to_string(); + 'outer: loop { + let mut current = formula.to_string(); -// loop { -// let old_formula_replaced = formula_replaced.clone(); + conversion_count = 0; + rand_conversions.shuffle(&mut rng); -// rand_conversions.shuffle(&mut rng); + while current != "e" { + let mut reduced_this_round = false; + for (short, long) in conversions { + if let Some(pos) = current.find(long) { + conversion_count += 1; + reduced_this_round = true; + current.replace_range(pos..pos + long.len(), short); + break; + } + } -// for (short, long) in conversions { -// if formula_replaced.contains(long) { -// conversion_count += 1; -// } - -// formula_replaced = replace_any(formula_replaced, long, short, &mut rng); -// } -// if formula_replaced == old_formula_replaced { -// break; -// } -// } -// if conversion_count < old_conversion_count { -// println!("{conversion_count}"); -// old_conversion_count = conversion_count; -// } -// } -// } + if !reduced_this_round { + continue 'outer; + } + } + println!("{conversion_count}"); + } +} // fn replace_any(input: String, long: &str, short: &str, rng: &mut ThreadRng) -> String { // let haystack: Vec<_> = input.chars().collect(); @@ -146,6 +145,8 @@ fn part1(conversions: &[(&str, &str)], formula: &[String]) { use std::fs; +use rand::seq::SliceRandom; + /// A single replacement rule as it appears in the puzzle input: `from => to`. struct Rule { from: String,