Last active
February 19, 2016 14:52
-
-
Save hakjoon/de59d3ba43e2e9536f4b to your computer and use it in GitHub Desktop.
Robot date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import izip_longest | |
import random | |
def grouper(iterable, n, fillvalue=None): | |
"Collect data into fixed-length chunks or blocks" | |
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx | |
args = [iter(iterable)] * n | |
return izip_longest(fillvalue=fillvalue, *args) | |
def make_grid(rows, columns): | |
length = rows * columns | |
grouped = grouper(range(length), columns) | |
return list(grouped) | |
def randcord(total): | |
all = range(total) | |
head, tail = all[0], all[-1] | |
return random.randint(head, tail) | |
def date(x=6, y=6, display=False): | |
grid = make_grid(x, y) | |
if display: | |
for r in grid: | |
print r | |
robbitta_x, robbitta_y = randcord(x), randcord(y) | |
robbitta = grid[robbitta_x][robbitta_y] | |
print "robbitta is waiting at ({}, {})".format(robbitta_x, robbitta_y) | |
nuts = traverse(grid, robbitta=robbitta) | |
print "You have arrived!" | |
print "You collected {} nuts on the way".format(nuts) | |
## this is the actual program the rest is just envrionment building | |
def turn(grid): | |
new_grid = zip(*grid)[::-1] | |
return new_grid | |
def traverse(g, nuts=1, robbitta=0): | |
row = g[0] | |
if robbitta in row: | |
index = row.index(robbitta) | |
nuts += index | |
return nuts | |
nuts += len(row) | |
g.pop(0) | |
if len(g): | |
nuts = traverse(turn(g), nuts, robbitta) | |
return nuts | |
if __name__ == "__main__": | |
date() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment