-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
47 lines (39 loc) · 1.27 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
mod workflow;
mod rule;
mod part;
mod system;
use std::rc::Rc;
use crate::{system::SortingSystem, part::{Part,Unit}, rule::Action};
fn main() {
let (parts, system) = parse_puzzle_data("src/bin/day19/input.txt");
let t = std::time::Instant::now();
let sum = parts.iter()
.filter(|&&part|
system.process_part(part, "in") == Some(Action::Accept)
)
.map(|part| part.sum())
.sum::<Unit>();
println!("Part 1: Sum of approved parts: {sum} ({:?})", t.elapsed());
assert_eq!(sum,287_054);
let t = std::time::Instant::now();
let sum = system.total_combinations("in", &[1..4001, 1..4001, 1..4001, 1..4001]);
println!("Part 2: Total combinations: {sum}, ({:?})", t.elapsed());
assert_eq!(sum,131_619_440_296_497);
}
fn parse_puzzle_data(file: &str) -> (Rc<[Part]>, SortingSystem) {
let inp = std::fs::read_to_string(file)
.expect("cannot load data file");
let mut split = inp.split("\n\n");
let wfs = split
.next()
.unwrap()
.parse::<SortingSystem>()
.expect("Failed to parse workflows");
let parts = split
.next()
.unwrap()
.lines()
.map(|line| line.parse::<Part>().expect("msg") )
.collect::<Rc<[Part]>>();
(parts,wfs)
}