highlight marbles and powered wires, show marble contents and direction beside the grid
This commit is contained in:
parent
2387c4b87c
commit
64dd244459
2 changed files with 50 additions and 43 deletions
|
@ -29,3 +29,5 @@ P P
|
|||
- `I` Input: Outputs a marble with the next byte from the program input stream, if there are any
|
||||
- `0123456789` Digits: decimal digits are added to a marbles value after multiplying the marble with 10, this means that a marble passing over `123` would contain the number 123 afterwards. Digits are consumed by marbles passing over them.
|
||||
|
||||
|
||||
TODO: rename to something more unique, there are already multiple esolangs called Marble(s)
|
||||
|
|
91
src/main.rs
91
src/main.rs
|
@ -108,6 +108,7 @@ fn main() {
|
|||
.unwrap_or_default();
|
||||
|
||||
let mut interpreter = Machine::new(&source, input_data);
|
||||
print!("\x1B[2J"); // clear screen
|
||||
|
||||
loop {
|
||||
interpreter.show();
|
||||
|
@ -142,8 +143,6 @@ impl Machine {
|
|||
let (rows, marbles) = parse(source);
|
||||
let grid = Grid::new(rows);
|
||||
Self {
|
||||
// width: grid[0].len(),
|
||||
// height: grid.len(),
|
||||
grid,
|
||||
marbles,
|
||||
input,
|
||||
|
@ -172,27 +171,32 @@ impl Machine {
|
|||
|
||||
fn show(&self) {
|
||||
// dbg!(&self);
|
||||
// print!("\x1B[2J"); // clear screen
|
||||
print!("\x1B[2J"); // clear screen
|
||||
print!("\x1B[u"); // reset cursor
|
||||
for y in 0..self.grid.height() {
|
||||
let mut marbles_on_row = Vec::new();
|
||||
for x in 0..self.grid.width() {
|
||||
let tile = self.grid.get((x, y).into());
|
||||
if let Tile::Marble { value, dir } = tile {
|
||||
marbles_on_row.push((value, dir));
|
||||
print!("{}", 'o'.green());
|
||||
continue;
|
||||
}
|
||||
let mut powered = false;
|
||||
let c = match tile {
|
||||
// Tile::Marble { value: _, dir } => match dir {
|
||||
// Direction::Up => '~',
|
||||
// Direction::Down => 'u',
|
||||
// Direction::Left => '{',
|
||||
// Direction::Right => '}',
|
||||
// },
|
||||
Tile::Marble { value: _, dir: _ } => 'o',
|
||||
Tile::Marble { value: _, dir: _ } => unreachable!(),
|
||||
Tile::Blank => ' ',
|
||||
Tile::Block => '#',
|
||||
Tile::Bag { count: _ } => 'B',
|
||||
Tile::Trigger => '*',
|
||||
Tile::Wire(w, _state) => match w {
|
||||
WireType::Vertical => '|',
|
||||
WireType::Horizontal => '-',
|
||||
WireType::Cross => '+',
|
||||
},
|
||||
Tile::Wire(w, state) => {
|
||||
powered = state;
|
||||
match w {
|
||||
WireType::Vertical => '|',
|
||||
WireType::Horizontal => '-',
|
||||
WireType::Cross => '+',
|
||||
}
|
||||
}
|
||||
Tile::Gate(g) => match g {
|
||||
GateType::LessThan => 'L',
|
||||
GateType::GreaterThan => 'G',
|
||||
|
@ -213,15 +217,18 @@ impl Machine {
|
|||
MirrorType::Forward => '/',
|
||||
MirrorType::Back => '\\',
|
||||
},
|
||||
Tile::Arrow(d) => match d {
|
||||
Direction::Up => '^',
|
||||
Direction::Down => 'v',
|
||||
Direction::Left => '<',
|
||||
Direction::Right => '>',
|
||||
},
|
||||
Tile::Arrow(d) => d.char(),
|
||||
Tile::Digit(d) => d as char,
|
||||
};
|
||||
print!("{c}");
|
||||
if powered {
|
||||
print!("{}", c.red());
|
||||
} else {
|
||||
print!("{c}");
|
||||
}
|
||||
}
|
||||
print!(" :: ");
|
||||
for (val, dir) in marbles_on_row {
|
||||
print!("{}", format!("{}{:<3} ", dir.char(), val).green())
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
@ -232,6 +239,16 @@ impl Machine {
|
|||
|
||||
fn step_once(&mut self) {
|
||||
self.steps += 1;
|
||||
// reset wires
|
||||
for y in 0..self.grid.height() {
|
||||
for x in 0..self.grid.width() {
|
||||
if let Tile::Wire(_, state) = self.grid.get_mut((x, y).into()) {
|
||||
if *state {
|
||||
*state = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut to_remove = Vec::new();
|
||||
let mut triggers = Vec::new();
|
||||
for i in 0..self.marbles.len() {
|
||||
|
@ -344,19 +361,9 @@ impl Machine {
|
|||
self.propagate_power(dir, dir.step(pos));
|
||||
}
|
||||
}
|
||||
for y in 0..self.grid.height() {
|
||||
for x in 0..self.grid.width() {
|
||||
if let Tile::Wire(_, state) = self.grid.get_mut((x, y).into()) {
|
||||
if *state {
|
||||
*state = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn propagate_power(&mut self, dir: Direction, pos: Pos) {
|
||||
// let came_from = dir.opposite();
|
||||
if !self.grid.in_bounds(pos) {
|
||||
return;
|
||||
}
|
||||
|
@ -489,6 +496,15 @@ impl Direction {
|
|||
Direction::Right,
|
||||
];
|
||||
|
||||
fn char(&self) -> char {
|
||||
match self {
|
||||
Direction::Up => '^',
|
||||
Direction::Down => 'v',
|
||||
Direction::Left => '<',
|
||||
Direction::Right => '>',
|
||||
}
|
||||
}
|
||||
|
||||
fn opposite(&self) -> Direction {
|
||||
match self {
|
||||
Direction::Up => Direction::Down,
|
||||
|
@ -512,7 +528,6 @@ impl Direction {
|
|||
}
|
||||
|
||||
fn step(&self, mut pos: Pos) -> Pos {
|
||||
// dbg!(&x, &y);
|
||||
match self {
|
||||
Direction::Up => pos.y -= 1,
|
||||
Direction::Down => pos.y += 1,
|
||||
|
@ -521,16 +536,6 @@ impl Direction {
|
|||
}
|
||||
pos
|
||||
}
|
||||
|
||||
// fn step(&self, x: &mut usize, y: &mut usize) {
|
||||
// // dbg!(&x, &y);
|
||||
// match self {
|
||||
// Direction::Up => *y -= 1,
|
||||
// Direction::Down => *y += 1,
|
||||
// Direction::Left => *x -= 1,
|
||||
// Direction::Right => *x += 1,
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
impl WireType {
|
||||
|
|
Loading…
Reference in a new issue