Skip to main content
added 1440 characters in body
Source Link
Martin Ender
  • 196.9k
  • 66
  • 446
  • 971

LabyrinthLabyrinth, 8 bytes

Take that, Pyth...

?+
;,;!@

Try it online!

Explanation

The usual primer (stolen from Sp3000):

  • Labyrinth is 2D and stack-based. Stacks have an infinite number of zeroes on the bottom.
  • When the instruction pointer reaches a junction, it checks the top of the stack to determine where to turn next. Negative is left, zero is forward and positive is right.

What comes in really handy here is that Labyrinth has two different input commands, , and ?. The former reads a single byte from STDIN, or -1 at EOF. The latter reads an integer from STDIN. It does so skipping everything that isn't a number and then reads the first decimal number it finds. This one returns 0 at EOF, so we can't use it to check for EOF reliably here.

The main loop of the program is this compact bit:

?+
;,

With ? we read an integer (ignoring all letters), with + we add it to the running total (which starts out as one of the implicit zeroes at the stack bottom). Then we read another character with , to check for EOF. As long as we're not at EOF, the read character will be a letter which has a positive character code, so the IP turns right (from its point of view; i.e. west). ; discards the character because we don't need it and then we enter the loop again.

Once we're at EOF, , pushes a -1 so the IP turns left (east) instead. ; again discards that -1, ! prints the running total as an integer and @ terminates the program.

Labyrinth, 8 bytes

Take that, Pyth...

?+
;,;!@

Try it online!

Labyrinth, 8 bytes

Take that, Pyth...

?+
;,;!@

Try it online!

Explanation

The usual primer (stolen from Sp3000):

  • Labyrinth is 2D and stack-based. Stacks have an infinite number of zeroes on the bottom.
  • When the instruction pointer reaches a junction, it checks the top of the stack to determine where to turn next. Negative is left, zero is forward and positive is right.

What comes in really handy here is that Labyrinth has two different input commands, , and ?. The former reads a single byte from STDIN, or -1 at EOF. The latter reads an integer from STDIN. It does so skipping everything that isn't a number and then reads the first decimal number it finds. This one returns 0 at EOF, so we can't use it to check for EOF reliably here.

The main loop of the program is this compact bit:

?+
;,

With ? we read an integer (ignoring all letters), with + we add it to the running total (which starts out as one of the implicit zeroes at the stack bottom). Then we read another character with , to check for EOF. As long as we're not at EOF, the read character will be a letter which has a positive character code, so the IP turns right (from its point of view; i.e. west). ; discards the character because we don't need it and then we enter the loop again.

Once we're at EOF, , pushes a -1 so the IP turns left (east) instead. ; again discards that -1, ! prints the running total as an integer and @ terminates the program.

Source Link
Martin Ender
  • 196.9k
  • 66
  • 446
  • 971

Labyrinth, 8 bytes

Take that, Pyth...

?+
;,;!@

Try it online!