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.
#!/usr/bin/env python
# NyanFlip - An extension of BackFlip by Peter Ward (flowblok).
# Implemented by David Vo (auscompgeek) and Jonathan Grigg.
import sys
ARROWS = '<V^>'
LEFT, DOWN, UP, RIGHT = ARROWS
def opp(direction):
if direction == UP:
return DOWN
if direction == DOWN:
return UP
if direction == LEFT:
return RIGHT
if direction == RIGHT:
return LEFT
def nyanflip(grid):
height = len(grid)
width = len(grid[0])
x, y = 0, 0
direction = RIGHT
score = 0
while (0 <= x < width) and (0 <= y < height):
tile = grid[y][x]
if tile in ARROWS:
grid[y][x], direction = opp(direction), tile
elif tile == '\\':
if direction == DOWN:
direction = RIGHT
elif direction == UP:
direction = LEFT
elif direction == RIGHT:
direction = DOWN
elif direction == LEFT:
direction = UP
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
grid[y][x] = '\\'
elif tile == '+':
score += 1
elif tile == '-':
score -= 1
elif tile == '.':
print(score)
elif tile == ',':
sys.stdout.write(chr(score))
if direction == UP:
y -= 1
elif direction == DOWN:
y += 1
elif direction == LEFT:
x -= 1
elif direction == RIGHT:
x += 1
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
nyanflip([list(x) for x in lines])
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