Day 14 part 2 that compiles forever

This commit is contained in:
Bill Thiede 2021-12-14 19:47:05 -08:00
parent dbd597ad41
commit 0a2c2d13ce

View File

@ -1,14 +1,152 @@
use std::{ use std::collections::HashMap;
collections::HashMap,
fmt::{Debug, Error, Formatter},
num::ParseIntError,
ops::{Index, IndexMut},
str::FromStr,
};
use anyhow::Result; use anyhow::Result;
use aoc_runner_derive::{aoc, aoc_generator}; use aoc_runner_derive::aoc;
use thiserror::Error;
struct TupleWindow<I, T>
where
I: Iterator<Item = T>,
{
iter: I,
prev: Option<T>,
next: Option<T>,
}
impl<I, T> TupleWindow<I, T>
where
I: Iterator<Item = T>,
{
fn new(iter: I, rules: &HashMap<&[u8], u8>) -> Self {
TupleWindow {
iter,
prev: None,
next: None,
}
}
}
impl<I, T> Iterator for TupleWindow<I, T>
where
I: Iterator<Item = T>,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.prev.is_none() {
self.prev = self.iter.next();
}
/*
template.next() {
template.flat_map(|y|
let z = rules[xy];
res[i * 2] = xy[0];
res[i * 2 + 1] = z;
res[i * 2 + 2] = xy[1];
});
//dbg!(String::from_utf8_lossy(&res));
res
*/
if let Some(next) = self.iter.next() {
let prev = self.prev.take();
self.prev = Some(next);
return prev;
}
None
}
}
fn expand_it<'a, I: 'a + Iterator<Item = &'a u8>>(
template: I,
rules: &HashMap<&[u8], u8>,
) -> impl Iterator<Item = &'a u8> {
TupleWindow::new(template, rules)
}
fn forty_steps<'a, I: 'a + Iterator<Item = &'a u8>>(it: I, rules: &HashMap<&[u8], u8>) -> usize {
//let it = (1..40).fold(it, |acc, _| expand_it(acc, &rules));
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(
expand_it(it, &rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules),
&rules,
)
.count()
}
fn expand(template: &[u8], rules: &HashMap<&[u8], u8>) -> Vec<u8> { fn expand(template: &[u8], rules: &HashMap<&[u8], u8>) -> Vec<u8> {
let mut res = vec![0u8; template.len() * 2 - 1]; let mut res = vec![0u8; template.len() * 2 - 1];
@ -29,6 +167,13 @@ fn count(template: &[u8]) -> (usize, usize) {
*m.entry(*v).or_insert(0) += 1; *m.entry(*v).or_insert(0) += 1;
m m
}); });
let mut keys: Vec<_> = m.keys().collect();
keys.sort_unstable();
let mut s = "".to_string();
for k in keys {
s.push_str(&format!("{}: {} ", String::from_utf8_lossy(&[*k]), m[k]));
}
println!("Counts: {}", s);
m.values() m.values()
.fold((usize::MAX, 0), |(min, max), v| (min.min(*v), max.max(*v))) .fold((usize::MAX, 0), |(min, max), v| (min.min(*v), max.max(*v)))
} }
@ -46,19 +191,31 @@ fn part1(input: &str) -> Result<usize> {
let mut template = template.as_bytes().to_vec(); let mut template = template.as_bytes().to_vec();
for i in 1..11 { for i in 1..11 {
template = expand(&template, &rules); template = expand(&template, &rules);
//println!("After step {}: {}", i, String::from_utf8_lossy(&template)); let s = String::from_utf8_lossy(&template);
println!("After step {}: ({}) {}", i, s.len(), s);
count(&template);
} }
let (min, max) = count(&template); let (min, max) = count(&template);
Ok(max - min) Ok(max - min)
} }
/*
#[aoc(day14, part2)] #[aoc(day14, part2)]
fn part2(input: &[u64]) -> Result<u64> { fn part2(input: &str) -> Result<usize> {
todo!("part2"); let (template, rules) = input.split_once("\n\n").unwrap();
Ok(0) let rules: HashMap<&[u8], u8> = rules
.lines()
.map(|l| {
let (pair, insert) = l.split_once(" -> ").unwrap();
(pair.as_bytes(), insert.as_bytes()[0])
})
.collect();
let cnt = forty_steps(template.as_bytes().iter(), &rules);
dbg!(cnt);
//println!("After step {}: {}", i, String::from_utf8_lossy(&template));
//let (min, max) = count(template);
//Ok(max - min)
Ok(0)
} }
*/
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -67,38 +224,74 @@ mod tests {
#[test] #[test]
fn test_part1() -> Result<()> { fn test_part1() -> Result<()> {
let input = r#" let input = r#"
NNCB NNCB
CH -> B CH -> B
HH -> N HH -> N
CB -> H CB -> H
NH -> C NH -> C
HB -> C HB -> C
HC -> B HC -> B
HN -> C HN -> C
NN -> C NN -> C
BH -> H BH -> H
NC -> B NC -> B
NB -> B NB -> B
BN -> B BN -> B
BB -> N BB -> N
BC -> B BC -> B
CC -> N CC -> N
CN -> C CN -> C
"# "#
.trim(); .trim();
assert_eq!(part1(input)?, 1588); assert_eq!(part1(input)?, 1588);
Ok(()) Ok(())
} }
/*
#[test] #[test]
fn test_part2()->Result<()> { fn test_part2() -> Result<()> {
let input = r#" let input = r#"
"# NNCB
.trim();
assert_eq!(part2(&parse(input)?)?, u64::MAX); CH -> B
Ok(()) HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C
"#
.trim();
assert_eq!(part2(input)?, 2188189693529);
Ok(())
} }
*/
} }
// BB -> N BN NB BB NB NB BB
// BC -> B BB BC BN NB BB BC
// BH -> H BH HH BH HH HN NH
// BN -> B BB NB BN NB NB BB
// CB -> H CH HB \
// CC -> N CN NC
// CH -> B CB BH
// CN -> C CC CN
// HB -> C HC CB
// HC -> B HB BC
// HH -> N HN NH
// HN -> C HC CN
// NB -> B NB BB
// NC -> B NC BC
// NH -> C NC CH
// NN -> C NC CN
//
//
//