-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2010-BIO-2.py
More file actions
158 lines (136 loc) · 4.71 KB
/
2010-BIO-2.py
File metadata and controls
158 lines (136 loc) · 4.71 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Die Tipping
import copy
class Die:
faces = { # Axis direction; neg. then pos. - default orientation - store all faces as multiple orientations with same top
"x": (3, 4),
"y": (5, 2),
"z": (6, 1),
}
def turn(self, orbit_axis, clockwise): # As viewed from neg end of orbital axis:
# Get axes to change - orbit axis "stays still"
AXIS_NAMES = ["x", "y", "z"]
orbit_axis_index = AXIS_NAMES.index(orbit_axis)
axis_a_id, axis_b_id = tuple(AXIS_NAMES[orbit_axis_index+1:] + AXIS_NAMES[:orbit_axis_index]) # View from neg of orbital - up then right (90deg clockwise)
"""
^ = a
|_
o_|____> = b"""
# Axis going up then axis going right as viewed from neg end of orbital
axis_a = self.faces[axis_a_id]
axis_b = self.faces[axis_b_id]
if(not clockwise):
# Invert a as anticlockwise
axis_a = (axis_a[1], axis_a[0])
else:
# Invert b as clockwise
axis_b = (axis_b[1], axis_b[0])
# Save axes swapped
self.faces[axis_a_id] = axis_b
self.faces[axis_b_id] = axis_a
def tip(self, direction):
# Turn a certain direction on a 2D board - 0-thru-3 means up-clockwise-left
# Up/down so tip x axis
if(direction == 0):
# Up
self.turn("x", False)
elif(direction == 2):
# Down
self.turn("x", True)
# Right/left so tip y axis
elif (direction == 3):
# Left
self.turn("y", False)
elif (direction == 1):
# Right
self.turn("y", True)
def top(self):
# Positive z-axis
return self.faces["z"][1]
class Grid:
def __init__(self, centre):
self.die = Die()
self.grid = [] # 11 rows of 11 ones
for y in range(11):
self.grid.append(list((1, )*11))
self.width = 11
self.height = 11
# Replace centre of grid coordinate by coordinate - centre size 3by3
for cy in range(3):
for cx in range(3):
self.grid[cy+4][cx+4] = copy.deepcopy(centre[cy][cx]) # Offset of 4 so at centre (5 either side of centre 1, minus 1 each side to make 3)
# Start at centre
self.x = 5
self.y = 5
self.heading = 0 # Default = up
def move(self):
# Change value of current square
which_move = self.change_square()
if(which_move == 1 or which_move == 6):
# Move according to heading
self.move_raw()
elif(which_move == 2):
self.heading = (self.heading + 1) % 4
self.move_raw() # +90deg
elif (which_move == 3 or which_move == 4):
self.heading = (self.heading + 2) % 4
self.move_raw() # +180deg
elif (which_move == 5):
self.heading = (self.heading - 1) % 4
self.move_raw() # -90deg
def move_raw(self):
self.die.tip(self.heading)
# 0 or 2 = up or down
if(self.heading == 0):
self.y -= 1
elif(self.heading == 2):
self.y += 1
# 1 or 3 = right or left
if (self.heading == 3):
self.x -= 1
elif (self.heading == 1):
self.x += 1
self.y %= self.height
self.x %= self.width
def change_square(self):
value = self.grid[self.y][self.x]
# Add die and make <= to 6
value += self.die.top()
if value > 6: value -= 6 # Not mod as no 0
# Save value back to grid and return
self.grid[self.y][self.x] = value
return value
def three_by_three(self):
grid = []
# Return 3by3 grid section around current pos
for y in range(self.y-1, self.y+2): # Before, this and after
if(y < 0 or y >= self.height):
row = ["x", "x", "x"]
else:
row = []
for x in range(self.x - 1, self.x + 2): # Before, this and after
if (x < 0 or x >= self.width):
row.append("x")
else:
row.append(self.grid[y][x])
grid.append(row)
return grid
def input_grid(rows, cast):
grid = []
for i in range(rows):
row = list(map(cast, input().split()))
grid.append(row)
return grid
def print_grid(grid):
for row in grid:
print(" ".join(map(str, row))) # Turn each square into a str and join w/ spaces
grid = Grid(input_grid(3, int))
exit = False
while(not exit):
moves = int(input())
if(moves == 0):
exit = True
else:
for i in range(moves):
grid.move()
print_grid(grid.three_by_three())
input()