remove counter from bag tile, they can output unlimited marbles now

This commit is contained in:
Crispy 2024-09-30 20:59:00 +02:00
parent 1f9d07c862
commit b9009021ec
3 changed files with 20 additions and 29 deletions

View file

@ -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.

View file

@ -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)

View file

@ -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<Tile>>, Vec<Pos>) {
'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;