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
|
- `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.
|
- `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();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let mut interpreter = Machine::new(&source, input_data);
|
let mut interpreter = Machine::new(&source, input_data);
|
||||||
|
print!("\x1B[2J"); // clear screen
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
interpreter.show();
|
interpreter.show();
|
||||||
|
@ -142,8 +143,6 @@ impl Machine {
|
||||||
let (rows, marbles) = parse(source);
|
let (rows, marbles) = parse(source);
|
||||||
let grid = Grid::new(rows);
|
let grid = Grid::new(rows);
|
||||||
Self {
|
Self {
|
||||||
// width: grid[0].len(),
|
|
||||||
// height: grid.len(),
|
|
||||||
grid,
|
grid,
|
||||||
marbles,
|
marbles,
|
||||||
input,
|
input,
|
||||||
|
@ -172,27 +171,32 @@ impl Machine {
|
||||||
|
|
||||||
fn show(&self) {
|
fn show(&self) {
|
||||||
// dbg!(&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() {
|
for y in 0..self.grid.height() {
|
||||||
|
let mut marbles_on_row = Vec::new();
|
||||||
for x in 0..self.grid.width() {
|
for x in 0..self.grid.width() {
|
||||||
let tile = self.grid.get((x, y).into());
|
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 {
|
let c = match tile {
|
||||||
// Tile::Marble { value: _, dir } => match dir {
|
Tile::Marble { value: _, dir: _ } => unreachable!(),
|
||||||
// Direction::Up => '~',
|
|
||||||
// Direction::Down => 'u',
|
|
||||||
// Direction::Left => '{',
|
|
||||||
// Direction::Right => '}',
|
|
||||||
// },
|
|
||||||
Tile::Marble { value: _, dir: _ } => 'o',
|
|
||||||
Tile::Blank => ' ',
|
Tile::Blank => ' ',
|
||||||
Tile::Block => '#',
|
Tile::Block => '#',
|
||||||
Tile::Bag { count: _ } => 'B',
|
Tile::Bag { count: _ } => 'B',
|
||||||
Tile::Trigger => '*',
|
Tile::Trigger => '*',
|
||||||
Tile::Wire(w, _state) => match w {
|
Tile::Wire(w, state) => {
|
||||||
WireType::Vertical => '|',
|
powered = state;
|
||||||
WireType::Horizontal => '-',
|
match w {
|
||||||
WireType::Cross => '+',
|
WireType::Vertical => '|',
|
||||||
},
|
WireType::Horizontal => '-',
|
||||||
|
WireType::Cross => '+',
|
||||||
|
}
|
||||||
|
}
|
||||||
Tile::Gate(g) => match g {
|
Tile::Gate(g) => match g {
|
||||||
GateType::LessThan => 'L',
|
GateType::LessThan => 'L',
|
||||||
GateType::GreaterThan => 'G',
|
GateType::GreaterThan => 'G',
|
||||||
|
@ -213,15 +217,18 @@ impl Machine {
|
||||||
MirrorType::Forward => '/',
|
MirrorType::Forward => '/',
|
||||||
MirrorType::Back => '\\',
|
MirrorType::Back => '\\',
|
||||||
},
|
},
|
||||||
Tile::Arrow(d) => match d {
|
Tile::Arrow(d) => d.char(),
|
||||||
Direction::Up => '^',
|
|
||||||
Direction::Down => 'v',
|
|
||||||
Direction::Left => '<',
|
|
||||||
Direction::Right => '>',
|
|
||||||
},
|
|
||||||
Tile::Digit(d) => d as 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!();
|
println!();
|
||||||
}
|
}
|
||||||
|
@ -232,6 +239,16 @@ impl Machine {
|
||||||
|
|
||||||
fn step_once(&mut self) {
|
fn step_once(&mut self) {
|
||||||
self.steps += 1;
|
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 to_remove = Vec::new();
|
||||||
let mut triggers = Vec::new();
|
let mut triggers = Vec::new();
|
||||||
for i in 0..self.marbles.len() {
|
for i in 0..self.marbles.len() {
|
||||||
|
@ -344,19 +361,9 @@ impl Machine {
|
||||||
self.propagate_power(dir, dir.step(pos));
|
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) {
|
fn propagate_power(&mut self, dir: Direction, pos: Pos) {
|
||||||
// let came_from = dir.opposite();
|
|
||||||
if !self.grid.in_bounds(pos) {
|
if !self.grid.in_bounds(pos) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -489,6 +496,15 @@ impl Direction {
|
||||||
Direction::Right,
|
Direction::Right,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
fn char(&self) -> char {
|
||||||
|
match self {
|
||||||
|
Direction::Up => '^',
|
||||||
|
Direction::Down => 'v',
|
||||||
|
Direction::Left => '<',
|
||||||
|
Direction::Right => '>',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn opposite(&self) -> Direction {
|
fn opposite(&self) -> Direction {
|
||||||
match self {
|
match self {
|
||||||
Direction::Up => Direction::Down,
|
Direction::Up => Direction::Down,
|
||||||
|
@ -512,7 +528,6 @@ impl Direction {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn step(&self, mut pos: Pos) -> Pos {
|
fn step(&self, mut pos: Pos) -> Pos {
|
||||||
// dbg!(&x, &y);
|
|
||||||
match self {
|
match self {
|
||||||
Direction::Up => pos.y -= 1,
|
Direction::Up => pos.y -= 1,
|
||||||
Direction::Down => pos.y += 1,
|
Direction::Down => pos.y += 1,
|
||||||
|
@ -521,16 +536,6 @@ impl Direction {
|
||||||
}
|
}
|
||||||
pos
|
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 {
|
impl WireType {
|
||||||
|
|
Loading…
Reference in a new issue