Skip to content

Commit

Permalink
day19 fold -
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsella committed Dec 19, 2023
1 parent d6bd3d6 commit 125baa4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Empty file added 2023/day19/map+sum
Empty file.
17 changes: 11 additions & 6 deletions 2023/day19/src/part2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ fn count_accepted(flows: &[Flow], current: &Action, mut ranges: [Vec<usize>; 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::<usize>()
+ count_accepted(flows, &flow.action, ranges)
}

pub fn part2(input: &str) -> usize {
Expand Down

0 comments on commit 125baa4

Please sign in to comment.