-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.rs
55 lines (42 loc) · 1.49 KB
/
7.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
48
49
50
51
52
53
54
55
use std::collections::HashMap;
type Assignments<'a> = HashMap<&'a str, &'a str>;
const INPUT: &str = include_str!("7.txt");
fn main() { one(); two(); }
fn one() {
let mut map = Assignments::new();
for line in INPUT.lines() {
let mut line = line.split(" -> ");
let from = line.next().unwrap();
let into = line.next().unwrap();
map.insert(into, from);
}
let mut level: i16 = 1;
let result = find_signal("a", &map, 1, &mut level);
println!("{result}");
}
fn find_signal(of: &str, map: &Assignments, cur: i16, level: &mut i16) -> i32 {
if cur > *level { *level = cur; }
println!("TODO: {level} - level of find_signal");
if let Ok(sig) = of.parse() { return sig; }
let gate: Vec<&str> = map.get(&of).unwrap().split_whitespace().collect();
match gate.len() {
// must be "<wire>"
1 => return find_signal(gate[0], map, cur + 1, level),
// must be "NOT <wire>"
2 => return -find_signal(gate[1], map, cur + 1, level),
// must be "<wire> AND/OR/RSHIFT/LSHIFT <wire>"
_ => {
let source_1 = find_signal(gate[0], map, cur + 1, level);
let source_2 = find_signal(gate[2], map, cur + 1, level);
return match gate[1] {
"AND" => source_1 & source_2,
"OR" => source_1 | source_2,
"LSHIFT" => source_1 << source_2,
_ => source_1 >> source_2
};
}
}
}
fn two() {
todo!()
}