summaryrefslogtreecommitdiff
path: root/src/bin/day7.rs
blob: 3bc4a811f4a5330e9966154b52f7ff24cd798bd1 (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
use std::collections::HashMap;
use regex::Regex;

fn main() {
    let input = advent::read_lines(7);

    let mut circuit = Circuit::new(&input);
    let signal_a = circuit.lookup_signal("a");
    println!("7a: {}", signal_a);

    let mut circuit = Circuit::new(&input);
    circuit.instructions.insert("b".to_string(), Operation::Direct(Signal::Raw(signal_a)));
    let signal_a = circuit.lookup_signal("a");
    println!("7b: {}", signal_a);
}

#[derive(Clone)]
enum Signal {
    Raw(u16),
    Wire(String),
}

impl Signal {
    fn new(input: &str) -> Signal {
        match input.parse::<u16>() {
            Ok(val) => Signal::Raw(val),
            Err(_) => Signal::Wire(input.to_string()),
        }
    }
}

#[derive(Clone)]
enum Operation {
    And(Signal, Signal),
    Or(Signal, Signal),
    Lshift(Signal, u8),
    Rshift(Signal, u8),
    Not(Signal),
    Direct(Signal),
}

struct Circuit {
    instructions: HashMap<String, Operation>,
}

impl Circuit {
    fn new<T: AsRef<str>>(input: &[T]) -> Circuit {
        let re_and = Regex::new(r"^([a-z0-9]+) AND ([a-z0-9]+) -> ([a-z0-9]+)").unwrap();
        let re_or = Regex::new(r"^([a-z0-9]+) OR ([a-z0-9]+) -> ([a-z0-9]+)").unwrap();
        let re_lshift = Regex::new(r"^([a-z0-9]+) LSHIFT ([0-9]+) -> ([a-z0-9]+)").unwrap();
        let re_rshift = Regex::new(r"^([a-z0-9]+) RSHIFT ([0-9]+) -> ([a-z0-9]+)").unwrap();
        let re_not = Regex::new(r"^NOT ([a-z0-9]+) -> ([a-z0-9]+)").unwrap();
        let re_direct = Regex::new(r"^([a-z0-9]+) -> ([a-z0-9]+)").unwrap();

        let mut instructions = HashMap::new();

        for line in input {
            let (out, operation) = if let Some(cap) = re_and.captures(line.as_ref()) {
                let s1 = Signal::new(&cap[1]);
                let s2 = Signal::new(&cap[2]);
                let out = cap[3].to_string();
                (out, Operation::And(s1, s2))
            } else if let Some(cap) = re_or.captures(line.as_ref()) {
                let s1 = Signal::new(&cap[1]);
                let s2 = Signal::new(&cap[2]);
                let out = cap[3].to_string();
                (out, Operation::Or(s1, s2))
            } else if let Some(cap) = re_lshift.captures(line.as_ref()) {
                let s1 = Signal::new(&cap[1]);
                let val = cap[2].parse::<u8>().unwrap();
                let out = cap[3].to_string();
                (out, Operation::Lshift(s1, val))
            } else if let Some(cap) = re_rshift.captures(line.as_ref()) {
                let s1 = Signal::new(&cap[1]);
                let val = cap[2].parse::<u8>().unwrap();
                let out = cap[3].to_string();
                (out, Operation::Rshift(s1, val))
            } else if let Some(cap) = re_not.captures(line.as_ref()) {
                let s1 = Signal::new(&cap[1]);
                let out = cap[2].to_string();
                (out, Operation::Not(s1))
            } else if let Some(cap) = re_direct.captures(line.as_ref()) {
                let s1 = Signal::new(&cap[1]);
                let out = cap[2].to_string();
                (out, Operation::Direct(s1))
            } else {
                panic!("invalid instruction: {}", line.as_ref());
            };
            instructions.insert(out, operation);
        }

        Circuit { instructions }
    }

    fn lookup_signal(&mut self, wire: &str) -> u16 {
        let res = match self.instructions.get(wire).unwrap().clone() {
            Operation::Direct(s) => {
                match s {
                    Signal::Raw(i) => i,
                    Signal::Wire(w) => self.lookup_signal(&w),
                }
            },
            Operation::And(s1, s2) => {
                let val1 = match s1 {
                    Signal::Raw(i) => i,
                    Signal::Wire(w) => self.lookup_signal(&w),
                };
                let val2 = match s2 {
                    Signal::Raw(i) => i,
                    Signal::Wire(w) => self.lookup_signal(&w),
                };
                val1 & val2
            },
            Operation::Or(s1, s2) => {
                let val1 = match s1 {
                    Signal::Raw(i) => i,
                    Signal::Wire(w) => self.lookup_signal(&w),
                };
                let val2 = match s2 {
                    Signal::Raw(i) => i,
                    Signal::Wire(w) => self.lookup_signal(&w),
                };
                val1 | val2
            },
            Operation::Lshift(s, amnt) => {
                let val = match s {
                    Signal::Raw(i) => i,
                    Signal::Wire(w) => self.lookup_signal(&w),
                };
                val << amnt
            },
            Operation::Rshift(s, amnt) => {
                let val = match s {
                    Signal::Raw(i) => i,
                    Signal::Wire(w) => self.lookup_signal(&w),
                };
                val >> amnt
            },
            Operation::Not(s) => {
                let val = match s {
                    Signal::Raw(i) => i,
                    Signal::Wire(w) => self.lookup_signal(&w),
                };
                val ^ 0xffff
            },
        };
        self.instructions.insert(wire.to_string(), Operation::Direct(Signal::Raw(res)));
        res
    }
}

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

    #[test]
    fn test() {
        let input = [
            "123 -> x",
            "456 -> y",
            "x AND y -> d",
            "x OR y -> e",
            "x LSHIFT 2 -> f",
            "y RSHIFT 2 -> g",
            "NOT x -> h",
            "NOT y -> i",
        ];
        let mut circuit = Circuit::new(&input);
        assert_eq!(circuit.lookup_signal("d"), 72);
        assert_eq!(circuit.lookup_signal("e"), 507);
        assert_eq!(circuit.lookup_signal("f"), 492);
        assert_eq!(circuit.lookup_signal("g"), 114);
        assert_eq!(circuit.lookup_signal("h"), 65412);
        assert_eq!(circuit.lookup_signal("i"), 65079);
        assert_eq!(circuit.lookup_signal("x"), 123);
        assert_eq!(circuit.lookup_signal("y"), 456);
    }
}