diff --git a/2023/day19/map+sum b/2023/day19/map+sum new file mode 100644 index 0000000..e69de29 diff --git a/2023/day19/src/part2.rs b/2023/day19/src/part2.rs index 43b3093..b574e1c 100644 --- a/2023/day19/src/part2.rs +++ b/2023/day19/src/part2.rs @@ -8,12 +8,17 @@ fn count_accepted(flows: &[Flow], current: &Action, mut ranges: [Vec; 4]) Action::Flow(name) => name, }; let flow = flows.iter().find(|f| &f.name == name).unwrap(); - flow.rules.iter().fold(0, |acc, rule| { - let index = "xmas".find(rule.part).unwrap(); - let mut valid = ranges.clone(); - (valid[index], ranges[index]) = ranges[index].iter().partition(|&&val| rule.is_valid(val)); - acc + count_accepted(flows, &rule.action, valid) - }) + count_accepted(flows, &flow.action, ranges) + flow.rules + .iter() + .map(|rule| { + let index = "xmas".find(rule.part).unwrap(); + let mut valid = ranges.clone(); + (valid[index], ranges[index]) = + ranges[index].iter().partition(|&&val| rule.is_valid(val)); + count_accepted(flows, &rule.action, valid) + }) + .sum::() + + count_accepted(flows, &flow.action, ranges) } pub fn part2(input: &str) -> usize {