fix update order dependent behavior of wires when flipped

This commit is contained in:
Crispy 2024-12-07 23:19:32 +01:00
parent 42a355d387
commit 78027aaaa0

View file

@ -17,6 +17,7 @@ pub struct Machine {
marbles: Vec<Pos>,
powered: Vec<Pos>,
events: Vec<Event>,
flipper_events: Vec<Pos>,
input: Vec<u8>,
input_index: usize,
@ -45,6 +46,7 @@ impl Machine {
marbles: Vec::new(),
powered: Vec::new(),
events: Vec::new(),
flipper_events: Vec::new(),
input,
input_index: 0,
output: Vec::new(),
@ -290,11 +292,31 @@ impl Machine {
}
// process triggers
self.flipper_events.clear();
for pos in triggers_activated {
for dir in Direction::ALL {
self.propagate_power(dir, dir.step(pos));
}
}
// process flipped tiles
for &p in &self.flipper_events {
let Some(target) = self.board.get_mut(p) else {
continue;
};
match target {
Tile::Powerable(PTile::Wire(wire_type), _) => {
*wire_type = match *wire_type {
WireType::Vertical => WireType::Horizontal,
WireType::Horizontal => WireType::Vertical,
WireType::Cross => WireType::Cross,
}
}
Tile::Mirror(mirror) => mirror.flip(),
Tile::Arrow(dir) => *dir = dir.opposite(),
_ => (),
};
}
}
fn propagate_power(&mut self, dir: Direction, pos: Pos) {
@ -359,21 +381,7 @@ impl Machine {
}
}
PTile::Flipper => {
let Some(target) = self.board.get_mut(front_pos) else {
return;
};
match target {
Tile::Powerable(PTile::Wire(wire_type), _) => {
*wire_type = match *wire_type {
WireType::Vertical => WireType::Horizontal,
WireType::Horizontal => WireType::Vertical,
WireType::Cross => WireType::Cross,
}
}
Tile::Mirror(mirror) => mirror.flip(),
Tile::Arrow(dir) => *dir = dir.opposite(),
_ => (),
};
self.flipper_events.push(front_pos);
}
PTile::Gate(gate) => {
let gate = *gate;