+68
-6
day10/src/main.rs
+68
-6
day10/src/main.rs
···
13
13
debug: bool,
14
14
}
15
15
16
-
fn mash_buttons(switchboard: &mut SwitchBoard, buttons: &Vec<Button>) {
16
+
fn mash_buttons(switchboard: &mut SwitchBoard, buttons: &[Button]) -> usize {
17
+
let mut total_presses = 0;
17
18
while !switchboard.is_solved() {
19
+
if total_presses > 100 {
20
+
println!("Max presses reached");
21
+
break;
22
+
}
18
23
for i in 0..switchboard.size {
19
24
if !switchboard.is_correct(i) {
20
-
println!("Lightbulb {} is incorrect", i);
21
-
let valid_buttons = buttons
25
+
// println!("Lightbulb {} is incorrect", i);
26
+
let mut valid_buttons = buttons
22
27
.iter()
23
28
.filter(|b| b.toggles(i))
24
29
.collect::<Vec<&Button>>();
30
+
valid_buttons.sort_by(|a, b| a.targets.len().cmp(&b.targets.len()));
31
+
let distance = switchboard.dist_from_solution();
32
+
println!(
33
+
"Current button options: {:?}, distance: {}",
34
+
valid_buttons, distance
35
+
);
36
+
for btn in valid_buttons.iter() {
37
+
btn.press(switchboard);
38
+
total_presses += 1;
39
+
if switchboard.dist_from_solution() < distance {
40
+
println!("Toggled button {:?}", btn);
41
+
println!("Switchboard: {}", switchboard);
42
+
break;
43
+
} else {
44
+
btn.press(switchboard);
45
+
}
46
+
}
47
+
}
48
+
}
49
+
}
50
+
51
+
total_presses
52
+
}
53
+
54
+
fn solve_switches(switchboard: &mut SwitchBoard, buttons: &[Button]) -> usize {
55
+
let mut opstack: Vec<Button> = Vec::new();
56
+
57
+
while !switchboard.is_solved() {
58
+
if opstack.len() > 10 {
59
+
println!("Failed in loop, {:?}", opstack);
60
+
panic!("Too many operations");
61
+
}
62
+
println!("Switchboard: {}", switchboard);
63
+
println!("Current state: {:?}", opstack);
64
+
// find first switch that is incorrect
65
+
let first_wrong = switchboard.first_wrong();
66
+
// if we find a wrong switch, get its index
67
+
if let Some(index) = first_wrong {
68
+
// find all buttons that toggle this index
69
+
let mut valid_buttons = buttons
70
+
.iter()
71
+
.filter(|b| b.toggles(index))
72
+
.collect::<Vec<&Button>>();
73
+
// don't press the same button twice in a row
74
+
valid_buttons.retain(|b| !opstack.contains(b));
75
+
if !valid_buttons.is_empty() {
76
+
opstack.push(valid_buttons[0].clone());
25
77
valid_buttons[0].press(switchboard);
26
-
println!("Toggled button {:?}", valid_buttons[0]);
27
-
println!("Switchboard: {}", switchboard);
78
+
} else {
79
+
let first_button = opstack[1].clone();
80
+
opstack.clear();
81
+
switchboard.reset();
82
+
println!(
83
+
"Reset switchboard, first operation will be {:?}",
84
+
first_button
85
+
);
86
+
87
+
first_button.press(switchboard);
88
+
opstack.push(first_button.clone());
28
89
}
29
90
}
30
91
}
92
+
opstack.len()
31
93
}
32
94
33
95
fn main() {
···
47
109
buttons.push(button);
48
110
}
49
111
50
-
mash_buttons(&mut switchboard, &buttons);
112
+
solve_switches(&mut switchboard, &buttons);
51
113
}
52
114
53
115
println!("{}", file);
+20
day10/src/switchboard.rs
+20
day10/src/switchboard.rs
···
31
31
}
32
32
}
33
33
34
+
pub fn reset(&mut self) {
35
+
self.lights = vec![false; self.size];
36
+
}
37
+
34
38
pub fn is_correct(&self, index: usize) -> bool {
35
39
self.lights[index] == self.target[index]
36
40
}
···
41
45
42
46
pub fn toggle(&mut self, i: usize) {
43
47
self.lights[i] = !self.lights[i];
48
+
}
49
+
50
+
pub fn dist_from_solution(&self) -> usize {
51
+
self.lights
52
+
.iter()
53
+
.zip(self.target.iter())
54
+
.filter(|&(a, b)| a != b)
55
+
.count()
56
+
}
57
+
58
+
pub fn first_wrong(&self) -> Option<usize> {
59
+
self.lights
60
+
.iter()
61
+
.zip(self.target.iter())
62
+
.enumerate()
63
+
.position(|(_, (a, b))| a != b)
44
64
}
45
65
}
46
66