diff --git a/README.md b/README.md index 66e83ea..5d26841 100644 --- a/README.md +++ b/README.md @@ -16,15 +16,15 @@ P P ``` ## symbols -- `^v<>` Arrows: changes a marbles direction and moves it to the front, as long as there is a free space in the target location (or the marble is already at the target location) -- `\/` Mirrors: reflects marbles like an arrow, but depending on the incoming direction -- `#` Blocks: block marbles from moving further, without changing their direction -- `|-+` Wires: powers adjacent tiles when powered, in the directions indicated -- `*` Trigger: powers all 4 adjacent tiles when a marble moves over it -- `F` Flipper: flips the direction of a wire, arrow and mirror tiles when powered -- `B` Bag: consumes marbles that move into it, and keeps count of how many marbles it has. When powered, it emits an empty (0) marble moving away from the bag. -- `ASMDR` Math: Add/Subtract/Multiply/Divide/Remainder of the marble on the left and the right relative to the powered direction. Outputs a marble forward as long as at least one input exists -- `LG=!` Logic gates: compare the left and right input (both treated as 0 if absent) and power forward if Left is Less/Greater/Equal/Not Equal with Right +- `^v<>` Arrows: changes a marbles direction and moves it to the front, as long as there is a free space in the target location (or the marble is already at the target location, making it effectively bounce) +- `\/` Mirrors: reflects marbles like an arrow, but depends on the incoming direction +- `(text)`, `#` Comments: stops marbles from moving +- `|-+` Wires: powers adjacent tiles when powered, in the directions indicated. +- `*` Trigger: powers all 4 adjacent tiles when a marble moves over it. +- `F` Flipper: flips the direction of a wire, arrow and mirror tiles when powered. +- `B` Bag: consumes marbles that move into it, and emits an empty (0) marble when powered. +- `ASMDR` Math: Add/Subtract/Multiply/Divide/Remainder of the marble on the left and the right, relative to the power direction. Outputs a marble forward if at least one input exists. +- `LG=!` Logic gates: (Less/Greater/Equal/Not Equal) compares the left and right input (both treated as 0 if absent) when powered, and powers forward if the result is true. - `P` Print: Prints the value of the front marble when powered - `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. diff --git a/programs/adder.mbl b/programs/adder.mbl index 397aa9d..d01f5c4 100644 --- a/programs/adder.mbl +++ b/programs/adder.mbl @@ -8,7 +8,7 @@ > * B o (check if 0) - o 1 + 1 v < B=- 0 v o * D- (div/10) v B * < @@ -19,7 +19,7 @@ v> * 1 o ^ | # < #| -| v < - | *-|o + | *-| F +B * ^ #< \F---- (ascii digit s) diff --git a/src/main.rs b/src/main.rs index 1f2f935..7514e56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,11 +21,8 @@ struct Machine { enum Tile { #[default] Blank, - Block, Comment(u8), - Bag { - count: u8, - }, + Bag, Marble { value: u8, dir: Direction, @@ -192,8 +189,7 @@ impl Machine { Tile::Marble { value: _, dir: _ } => unreachable!(), Tile::Comment(_) => unreachable!(), Tile::Blank => ' ', - Tile::Block => '#', - Tile::Bag { count: _ } => 'B', + Tile::Bag => 'B', Tile::Trigger => '*', Tile::Wire(w, state) => { powered = state; @@ -283,8 +279,7 @@ impl Machine { self.marbles[i] = next_pos; new_tile = Some(Tile::Blank); } - Tile::Bag { count } => { - *count += 1; + Tile::Bag => { to_remove.push(i); new_tile = Some(Tile::Blank); } @@ -386,13 +381,10 @@ impl Machine { self.output.push(value); } } - Tile::Bag { count } => { - if *count > 0 { - *count -= 1; - if self.grid.get(front_pos).is_blank() { - *self.grid.get_mut(front_pos) = Tile::Marble { value: 0, dir }; - self.marbles.push(front_pos); - } + Tile::Bag => { + if self.grid.get(front_pos).is_blank() { + *self.grid.get_mut(front_pos) = Tile::Marble { value: 0, dir }; + self.marbles.push(front_pos); } } Tile::Input => { @@ -469,7 +461,6 @@ impl Machine { | Tile::Mirror(_) | Tile::Arrow(_) | Tile::Comment(_) - | Tile::Block | Tile::Blank => (), } } @@ -620,9 +611,9 @@ fn parse(source: &str) -> (Vec>, Vec) { 'M' => Tile::Math(MathOp::Mul), 'D' => Tile::Math(MathOp::Div), 'R' => Tile::Math(MathOp::Rem), - 'B' => Tile::Bag { count: 0 }, + 'B' => Tile::Bag, d @ '0'..='9' => Tile::Digit(d as u8), - '#' => Tile::Block, + '#' => Tile::Comment(b'#'), ' ' => Tile::Blank, '(' => { in_comment = true;