generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.rs
147 lines (117 loc) · 3.78 KB
/
21.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
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
advent_of_code::solution!(21);
use regex::Regex;
use std::collections::HashMap;
type NodeId = [u8; 4];
enum Operator {
Mul,
Div,
Add,
Sub,
}
enum Node {
ValueNode(u64),
EquationNode(NodeId, NodeId, Operator),
}
fn parse_data(input: &str) -> HashMap<NodeId, Node> {
let re_str_equation: &str = r"^(\w{4}): (\w{4}) ([+\-*/]) (\w{4})$";
let re_equation = Regex::new(re_str_equation).unwrap();
let re_str_value: &str = r"(^\w{4}): (\d+)$";
let re_value = Regex::new(re_str_value).unwrap();
input
.lines()
.map(|x| {
if x.len() == 17 {
let captures = re_equation.captures(x).unwrap();
let left = captures[2].as_bytes().try_into().unwrap();
let right = captures[4].as_bytes().try_into().unwrap();
let operator = match &captures[3] {
"+" => Operator::Add,
"-" => Operator::Sub,
"*" => Operator::Mul,
"/" => Operator::Div,
_ => unreachable!(),
};
let key = captures[1].as_bytes().try_into().unwrap();
let value = Node::EquationNode(left, right, operator);
(key, value)
} else {
let captures = re_value.captures(x).unwrap();
let key = captures[1].as_bytes().try_into().unwrap();
let value = Node::ValueNode(captures[2].parse().unwrap());
(key, value)
}
})
.collect()
}
fn solve_value(value: &u64) -> f64 {
*value as f64
}
fn solve_equation(
data: &HashMap<NodeId, Node>,
left: &NodeId,
right: &NodeId,
op: &Operator,
) -> f64 {
let left_solved = solve_node(data, left);
let right_solved = solve_node(data, right);
match op {
Operator::Mul => left_solved * right_solved,
Operator::Div => left_solved / right_solved,
Operator::Add => left_solved + right_solved,
Operator::Sub => left_solved - right_solved,
}
}
fn solve_node(data: &HashMap<NodeId, Node>, node_id: &NodeId) -> f64 {
let node = &data[node_id];
match node {
Node::ValueNode(v) => solve_value(v),
Node::EquationNode(left, right, op) => solve_equation(data, left, right, op),
}
}
pub fn part_one(input: &str) -> Option<u64> {
let data = parse_data(input);
let root_id: NodeId = "root".as_bytes().try_into().unwrap();
let result = solve_node(&data, &root_id) as u64;
Some(result)
}
pub fn part_two(input: &str) -> Option<u64> {
let mut data = parse_data(input);
let root_id: NodeId = "root".as_bytes().try_into().unwrap();
let humn_id: NodeId = "humn".as_bytes().try_into().unwrap();
let (left_id, right_id) = match data[&root_id] {
Node::EquationNode(left, right, _) => (left, right),
Node::ValueNode(_) => return None,
};
let right = solve_node(&data, &right_id);
let mut a = 0;
let mut b = u64::MAX;
loop {
let c = (a + b) / 2;
data.insert(humn_id, Node::ValueNode(c));
let left_c = solve_node(&data, &left_id) - right;
if left_c == 0_f64 {
return Some(c);
}
data.insert(humn_id, Node::ValueNode(a));
let left_a = solve_node(&data, &left_id) - right;
if left_a.signum() == left_c.signum() {
a = c;
} else {
b = c;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(152));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(301));
}
}