Skip to content

Commit

Permalink
day 19: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
anisse committed Dec 19, 2023
1 parent fd2cfd5 commit 6e553c3
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/bin/day19.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {
let res = part1(&wf, parts.clone());
println!("Part 1: {}", res);
//part 2
let res = part2(&wf, parts);
let res = part2(&wf);
println!("Part 2: {}", res);
}
type ParsedItem = Part;
Expand Down Expand Up @@ -35,11 +35,11 @@ fn parse(input: &str) -> (Workflows, impl Iterator<Item = ParsedItem> + Clone +
let mut parts = l.split(|c| c == '{' || c == '}');
let name = parts.next().expect("a name");
let rs = parts.next().expect("a part list");
println!("{name}, {rs}");
//println!("{name}, {rs}");
let rules = rs
.split(',')
.map(|r| {
println!("Parsing rule {r}");
//println!("Parsing rule {r}");
let (exp, dest) = if r.contains(':') {
let (es, dest) = r.split_once(':').expect("rule in two part");
let esb = es.as_bytes();
Expand Down Expand Up @@ -120,10 +120,7 @@ struct Accepted {
max: usize,
}

fn part2<I>(wf: &Workflows, parts: I) -> usize
where
I: Iterator<Item = ParsedItem>,
{
fn part2(wf: &Workflows) -> usize {
accept2(wf)
}
fn accept2(wf: &Workflows) -> usize {
Expand All @@ -133,9 +130,9 @@ fn accept2(wf: &Workflows) -> usize {

fn accept2_tree(wf: &Workflows, name: &str, mut ranges: [Accepted; 4]) -> Option<usize> {
let mut res = None;
println!("Evaluating rule {name}");
//println!("Evaluating rule {name}");
for rule in &wf[name] {
println!("dest is {}", rule.dest);
//println!("dest is {}", rule.dest);
let mut new = ranges;
if let Some(exp) = &rule.exp {
match exp.op {
Expand Down Expand Up @@ -164,7 +161,7 @@ fn eval_dest(res: &mut Option<usize>, wf: &Workflows, name: &str, ranges: &[Acce
Some(
ranges
.iter()
.inspect(|r| println!("Accepted: {r:?}"))
//.inspect(|r| println!("Accepted: {r:?}"))
.map(|r| if r.max < r.min { 0 } else { r.max - r.min })
//.map(|r| r.max - r.min)
.product::<usize>(),
Expand Down Expand Up @@ -198,6 +195,6 @@ fn test() {
let res = part1(&wf, parts.clone());
assert_eq!(res, 19114);
//part 2
let res = part2(&wf, parts);
let res = part2(&wf);
assert_eq!(res, 167409079868000);
}

0 comments on commit 6e553c3

Please sign in to comment.