Skip to content

Commit e065c58

Browse files
committed
refactor(07/2015): introduce signal trait
1 parent 2505de3 commit e065c58

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/solutions/year2015/day07.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,39 @@ impl Solution for Day07 {
1313

1414
fn part_two(&self, input: &str) -> String {
1515
let instructions = self.parse(input);
16-
let a_value = self.signal_for_instructions(&instructions.clone(), "a");
16+
let a_value = self.signal(&instructions.clone(), "a");
1717

1818
let mut instructions = instructions;
1919
instructions
2020
.entry("b".to_string())
2121
.and_modify(|v| *v = Instruction::Value(Value::Numeric(a_value.parse().unwrap())));
2222

23-
self.signal_for_instructions(&instructions, "a")
23+
self.signal(&instructions, "a")
2424
}
2525
}
2626

27-
impl Day07 {
28-
fn signal(&self, input: &str, wire: &str) -> String {
29-
let instructions = self.parse(input);
27+
trait Signal<T> {
28+
fn signal(&self, value: T, wire: &str) -> String;
29+
}
30+
31+
impl Signal<&str> for Day07 {
32+
fn signal(&self, value: &str, wire: &str) -> String {
33+
let instructions = self.parse(value);
3034

31-
self.signal_for_instructions(&instructions, wire)
35+
self.signal(&instructions, wire)
3236
}
37+
}
3338

34-
fn signal_for_instructions(&self, instructions: &Wires, wire: &str) -> String {
39+
impl Signal<&Wires> for Day07 {
40+
fn signal(&self, value: &Wires, wire: &str) -> String {
3541
let mut cache = HashMap::new();
36-
let main = instructions.get(wire).unwrap();
42+
let main = value.get(wire).unwrap();
3743

38-
main.calculate(instructions, &mut cache).to_string()
44+
main.calculate(value, &mut cache).to_string()
3945
}
46+
}
4047

48+
impl Day07 {
4149
fn parse(&self, input: &str) -> Wires {
4250
input
4351
.lines()

0 commit comments

Comments
 (0)