summaryrefslogtreecommitdiff
path: root/src/bin/day19.rs
blob: 89eb7dd92268cd818524c163035ca98b4962b51b (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use std::collections::HashMap;

static DAY: u8 = 19;

fn main() {
    let input = advent::read_lines(DAY);
    println!("{DAY}a: {}", rating_numbers(&input));
    println!("{DAY}b: {}", 0);
}

#[derive(Clone)]
enum WorkflowResult {
    Accept,
    Reject,
    NextWorkflow(String),
}

impl WorkflowResult {
    fn from(input: &str) -> WorkflowResult {
        match input {
            "A" => WorkflowResult::Accept,
            "R" => WorkflowResult::Reject,
            next => WorkflowResult::NextWorkflow(next.to_string()),
        }
    }
}

enum Rule {
    CmpGt(char, u32, WorkflowResult),
    CmpLt(char, u32, WorkflowResult),
    Result(WorkflowResult),
}

impl Rule {
    fn new(input: &str) -> Rule {
        if input.contains('<') {
            let tokens = input.split(|x| x == '<' || x == ':').collect::<Vec<_>>();
            let c = tokens[0].chars().next().unwrap();
            let val = tokens[1].parse().unwrap();
            let result = WorkflowResult::from(tokens[2]);
            Rule::CmpLt(c, val, result)
        } else if input.contains('>') {
            let tokens = input.split(|x| x == '>' || x == ':').collect::<Vec<_>>();
            let c = tokens[0].chars().next().unwrap();
            let val = tokens[1].parse().unwrap();
            let result = WorkflowResult::from(tokens[2]);
            Rule::CmpGt(c, val, result)
        } else {
            Rule::Result(WorkflowResult::from(input))
        }
    }

    fn matches(&self, xmas: &XmasValues) -> Option<WorkflowResult> {
        match self {
            Rule::CmpGt(variable, value, result) => match variable {
                'x' => if xmas.x > *value { Some(result.clone()) } else { None },
                'm' => if xmas.m > *value { Some(result.clone()) } else { None },
                'a' => if xmas.a > *value { Some(result.clone()) } else { None },
                's' => if xmas.s > *value { Some(result.clone()) } else { None },
                _ => panic!("invalid char"),
            },
            Rule::CmpLt(variable, value, result) => match variable {
                'x' => if xmas.x < *value { Some(result.clone()) } else { None },
                'm' => if xmas.m < *value { Some(result.clone()) } else { None },
                'a' => if xmas.a < *value { Some(result.clone()) } else { None },
                's' => if xmas.s < *value { Some(result.clone()) } else { None },
                _ => panic!("invalid char"),
            }
            Rule::Result(result) => Some(result.clone()),
        }
    }
}

struct Workflow {
    name: String,
    rules: Vec<Rule>,
}

impl Workflow {
    fn new(input: &str) -> Workflow {
        let (name, rules_str) = input[0..input.len() - 1].split_once('{').unwrap();
        let rules = rules_str.split(',')
                             .map(Rule::new)
                             .collect::<Vec<_>>();
        let name = name.to_string();

        Workflow { name, rules }
    }

    fn matches(&self, xmas: &XmasValues) -> WorkflowResult {
        for rule in &self.rules {
            if let Some(result) = rule.matches(xmas) {
                return result.clone();
            }
        }
        panic!("no matching rule found");
    }
}

struct XmasValues {
    x: u32,
    m: u32,
    a: u32,
    s: u32,
}

impl XmasValues {
    fn new(input: &str) -> XmasValues {
        let mut xmas = XmasValues { x: 0, m: 0, a: 0, s: 0 };
        for string in input[1 .. input.len() - 1 ].split(',') {
            let (variable, value) = string.split_once('=').unwrap();
            let value = value.parse::<u32>().unwrap();
            match variable {
                "x" => xmas.x = value,
                "m" => xmas.m = value,
                "a" => xmas.a = value,
                "s" => xmas.s = value,
                _ => panic!("invalid variable"),
            }
        }
        xmas
    }

    fn rating(&self) -> u32 {
        self.x + self.m + self.a + self.s
    }
}

struct Workflows {
    workflows: HashMap<String, Workflow>,
}

impl Workflows {
    fn new(input: &[String]) -> Workflows {
        let workflows = input.iter()
                             .map(|workflow| Workflow::new(workflow))
                             .map(|workflow| (workflow.name.clone(), workflow))
                             .collect();
        Workflows { workflows }
    }

    fn is_accepted(&self, xmas: &XmasValues) -> bool {
        let mut workflow = &self.workflows["in"];
        loop {
            match workflow.matches(xmas) {
                WorkflowResult::Accept => return true,
                WorkflowResult::Reject => return false,
                WorkflowResult::NextWorkflow(next) => workflow = &self.workflows[&next],
            }
        }
    }
}

fn rating_numbers(input: &[String]) -> u32 {
    let blocks = input.split(|line| line.is_empty()).collect::<Vec<_>>();
    let (workflows, values) = (Workflows::new(blocks[0]), blocks[1]);
    let values = values.iter()
                       .map(|value| XmasValues::new(value))
                       .collect::<Vec<_>>();

    values.iter()
          .filter(|&value| workflows.is_accepted(value))
          .map(|value| value.rating())
          .sum()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        let input = [
            "px{a<2006:qkq,m>2090:A,rfg}",
            "pv{a>1716:R,A}",
            "lnx{m>1548:A,A}",
            "rfg{s<537:gd,x>2440:R,A}",
            "qs{s>3448:A,lnx}",
            "qkq{x<1416:A,crn}",
            "crn{x>2662:A,R}",
            "in{s<1351:px,qqz}",
            "qqz{s>2770:qs,m<1801:hdj,R}",
            "gd{a>3333:R,R}",
            "hdj{m>838:A,pv}",
            "",
            "{x=787,m=2655,a=1222,s=2876}",
            "{x=1679,m=44,a=2067,s=496}",
            "{x=2036,m=264,a=79,s=2244}",
            "{x=2461,m=1339,a=466,s=291}",
            "{x=2127,m=1623,a=2188,s=1013}",
        ].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
        assert_eq!(rating_numbers(&input), 19114);
    }
}