-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.rs
34 lines (31 loc) · 1.02 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
use regex::Regex;
use std::collections::HashMap;
pub fn main() {
let re = Regex::new(r#"^mem\[(\d+)\] = (\d+)$"#).unwrap();
let mut mem: HashMap<usize, usize> = HashMap::new();
let mut and_or = (0, 0);
for line in include_str!("../input.txt").lines() {
if line.starts_with("ma") {
and_or = line
.split(" = ")
.skip(1)
.next()
.unwrap()
.bytes()
.rev()
.enumerate()
.fold((usize::MAX, 0), |(and, or), (i, b)| match b {
b'0' => (and & !(1 << i), or),
b'1' => (and, or | 1 << i),
_ => (and, or),
});
} else {
let captures = re.captures(&line).unwrap();
mem.insert(
captures[1].parse().unwrap(),
captures[2].parse::<usize>().unwrap() & and_or.0 | and_or.1,
);
}
}
println!("{}", mem.values().sum::<usize>());
}