Skip to content

Instantly share code, notes, and snippets.

@auscompgeek
Last active January 8, 2016 11:14
Show Gist options
  • Save auscompgeek/f8695d7152e82e0fafc2 to your computer and use it in GitHub Desktop.
Save auscompgeek/f8695d7152e82e0fafc2 to your computer and use it in GitHub Desktop.
NyanFlip - An extension of BackFlip by Peter Ward (@flowblok). An example program is attached.

NyanFlip

An extension by Peter Ward (@flowblok) of the BackFlip esoteric programming language.

Here follows the original description by @flowblok from the NCSS 2014 Programming Competition:

Nyan Cat is stuck in an arcade game! You have the feeling that she's trying to get a message to you, but the game is running so slowly it will take days until she completes her message. The game is played on a 2D grid. Nyan Cat enters at the top-left corner, and starts by moving one tile to the right every turn. Some tiles have things displayed on them:

  • Arrows (<, >, ^ and V): When Nyan Cat steps on one of these, it changes her direction so she is now moving in the direction the arrow pointed in. However, that arrow then changes direction to point back the way she came from.
  • Mirrors (/ and \): When Nyan Cat hits a mirror, her direction is changed as if she was reflected off it, and the mirror changes to the other type of mirror. For example, if Nyan Cat is moving downwards and hits a / mirror, she will now be moving to the left, and it will now be a \ mirror.
  • Stars (+): When Nyan Cat hits a star, the game increases her score by one point (it starts at zero).
  • Clouds (-): When Nyan Cat hits a cloud, the game decreases her score by one point.
  • Checkpoints (.): When Nyan Cat hits a cloud [sic], the game prints out her score on a new line of a paper reel on the side of the arcade game.

When Nyan Cat goes off the side of the board, the game stops.

You think that Nyan Cat is writing her message by changing the game board so the checkpoints the game prints out spell a message when converted to letters (1 = A, 2 = B, ...).

I have since added three different 'checkpoints' to allow for proper I/O:

  • ,: Converts the score to its corresponding ASCII character.
  • *: Converts the score to letters according to the algorithm in the original description.
  • ~: Reads in one character and converts to its corresponding ASCII value.
#!/usr/bin/env python
# NyanFlip - An extension of BackFlip by Peter Ward (flowblok).
# Implemented by David Vo (auscompgeek) and Jonathan Grigg.
# Usage: ./nyanflip.py [board.txt] - uses stdin if file not specified
import sys
LEFT, DOWN, UP, RIGHT = ARROWS = '<V^>'
OPP_ARROW = dict(zip(ARROWS, reversed(ARROWS)))
class NyanFlip(object):
def __init__(self, lines):
self.grid = [list(x) for x in lines]
self.height = len(lines)
self.width = len(lines[0])
self.reset()
def __iter__(self):
return self
def __next__(self):
return self.step()
def reset(self):
self.x, self.y = 0, 0
self.direction = RIGHT
self.score = 0
def step(self):
x, y = self.x, self.y
if not ((0 <= x < self.width) and (0 <= y < self.height)):
raise StopIteration
tile = self.grid[y][x]
direction = self.direction
if tile in ARROWS:
self.grid[y][x], direction = OPP_ARROW[direction], tile
elif tile == '\\':
if direction == DOWN:
direction = RIGHT
elif direction == UP:
direction = LEFT
elif direction == RIGHT:
direction = DOWN
elif direction == LEFT:
direction = UP
self.grid[y][x] = '/'
elif tile == '/':
if direction == DOWN:
direction = LEFT
elif direction == UP:
direction = RIGHT
elif direction == RIGHT:
direction = UP
elif direction == LEFT:
direction = DOWN
self.grid[y][x] = '\\'
elif tile == '+':
self.score += 1
elif tile == '-':
self.score -= 1
elif tile == '.':
print(self.score)
elif tile == ',':
sys.stdout.write(chr(self.score))
elif tile == '*':
sys.stdout.write(chr(64+self.score))
elif tile == '~':
self.score = ord(sys.stdin.read(1))
if direction == UP:
self.y -= 1
elif direction == DOWN:
self.y += 1
elif direction == LEFT:
self.x -= 1
elif direction == RIGHT:
self.x += 1
self.direction = direction
return tile
def next(self):
return self.step()
def run(self):
for tile in self:
pass
def main():
if len(sys.argv) > 1:
with open(sys.argv[1]) as f:
lines = f.read().split('\n\n')[0].split('\n')
else:
read = raw_input if sys.version_info[0] < 3 else input
try:
lines, line = [], read()
while line:
lines.append(line)
line = read()
except EOFError:
pass
prog = NyanFlip(lines)
prog.run()
if __name__ == '__main__':
main()
\++*+\\ \V
+ >++ <\ V+>\+*V
+VV^ *- \ V
>\ +<+ - ^-\-<
>\\<> -\
>\ ^ >^- -
^ ^ ^ > -<
@auscompgeek
Copy link
Author

Version history

This interpreter

v0: initial release
v1: added README and * checkpoint
v2: refactor to allow stepping through the program
v3: added ~ checkpoint

The language

v-1: BackFlip
v0: as described for NCSS 2014 ProgComp
v1: added , checkpoint
v2: added * checkpoint
v3: added ~ checkpoint

@cyphar
Copy link

cyphar commented Jan 24, 2014

read = raw_input if sys.version_info[0] < 3 else input

Many enterprise, such wow.

@auscompgeek
Copy link
Author

Apparently this was recycled for this year's NCSS ProgComp. Well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment