/
random_solution.py
executable file
·61 lines (38 loc) · 1.14 KB
/
random_solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# Copyright (c) 2015 by Ken Guyton. All Rights Reserved.
"""Create a random grid and compute the solution."""
from __future__ import print_function
import walk_grid
import random
MAX_GRID_SIZE = 20
MAX_FOOD = 200
MAX_ROOM_FOOD = 10
def create_grid(grid_size):
"""Randomly create a grid."""
grid = []
for j in range(grid_size):
grid.append([])
for unused_i in range(grid_size):
grid[j].append(random.randrange(MAX_ROOM_FOOD + 1))
grid[0][0] = 0
return grid
def print_grid(grid):
"""Print out a grid."""
for row in grid:
for num in row:
print(' {0:2d}'.format(num), end='')
print()
def main():
"""Compute the answer for the given grid and food amounts."""
food = random.randrange(MAX_FOOD + 1)
print('Food: {0}.'.format(food))
grid_size = random.randrange(1, MAX_GRID_SIZE + 1)
print('Grid size: {0}.'.format(grid_size))
grid = create_grid(grid_size)
print('\nThe Grid...\n')
print_grid(grid)
print()
least_left, steps = walk_grid.answer_and_steps(grid, food)
print('\nResult: {0} with steps {1}.'.format(least_left, steps))
if __name__ == '__main__':
main()