-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlangton_ant.py
214 lines (178 loc) · 6.66 KB
/
langton_ant.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import dataclasses
import enum
from random import Random
from typing import Set, Dict, List
class Orientation(enum.Enum):
Left = "Left"
Right = "Right"
Up = "Up"
Down = "Down"
class Color(enum.Enum):
White = "white"
Black = "black"
class Ant:
def __init__(self):
self.position: Position = Position(2, 2)
self.orientation: Orientation = Orientation.Right
self.movement = []
def turn_clockwise(self):
if self.orientation == Orientation.Right:
self.orientation = Orientation.Down
elif self.orientation == Orientation.Down:
self.orientation = Orientation.Left
elif self.orientation == Orientation.Up:
self.orientation = Orientation.Right
elif self.orientation == Orientation.Left:
self.orientation = Orientation.Up
def turn_ccw(self):
if self.orientation == Orientation.Right:
self.orientation = Orientation.Up
elif self.orientation == Orientation.Down:
self.orientation = Orientation.Right
elif self.orientation == Orientation.Up:
self.orientation = Orientation.Left
elif self.orientation == Orientation.Left:
self.orientation = Orientation.Down
def forward(self):
if self.orientation == Orientation.Right:
self.position.col += 1
elif self.orientation == Orientation.Down:
self.position.row += 1
elif self.orientation == Orientation.Up:
self.position.row -= 1
elif self.orientation == Orientation.Left:
self.position.col -= 1
def __str__(self):
return f'Ant({self.position}, {self.orientation})'
@dataclasses.dataclass
class Position:
col: int
row: int
class Cell:
def __init__(self, color):
self.color: Color = color
@classmethod
def create(cls, color: Color):
return cls(color)
def __str__(self):
return '◽' if self.color == Color.White else '◾'
def as_html(self):
return '◾' if self.color == Color.Black else '◽'
class Board:
def __init__(self):
self.ant: Ant = Ant()
self.grid: List[List[Cell]] = []
for row in range(0, 4):
for col in range(0, 4):
if row > len(self.grid) - 1:
self.grid.append([])
cell = Cell(Color.White)
self.grid[row].append(cell)
def _get_cell_at(self, position: Position) -> Cell:
try:
return self.grid[position.row][position.col]
except Exception:
print('exception at ', position)
raise Exception()
@classmethod
def __generate_board(cls, old_grid: List[List[Cell]], keep_rows: bool) -> List[List[Cell]]:
grid: List[List[Cell]] = []
rows = len(old_grid)
cols = len(old_grid[0])
for row in range(0, rows if keep_rows else 3):
for col in range(0, 3 if keep_rows else cols):
if row > len(grid) - 1:
grid.append([])
cell = Cell(Color.White)
grid[row].append(cell)
return grid
def flip(self):
cell = self._get_cell_at(self.ant.position)
if cell.color is Color.White:
cell.color = Color.Black
else:
cell.color = Color.White
def move(self):
cell = self._get_cell_at(self.ant.position)
if cell.color == Color.White:
self.flip()
self.ant.turn_clockwise()
else:
self.flip()
self.ant.turn_ccw()
self.ant.forward() # Updates the position of the ant
last_col_index = len(self.grid[0]) - 1
if self.ant.position.col < 0:
self.__expand_left()
elif last_col_index < self.ant.position.col:
self.__expand_right()
last_row_index = len(self.grid) - 1
if self.ant.position.row < 0:
self.__expand_top()
elif last_row_index < self.ant.position.row:
self.__expand_down()
def __expand_right(self):
print(f"Expanding right {self.ant.position}")
board = Board.__generate_board(self.grid, True)
for row in range(0, len(self.grid)):
self.grid[row] = self.grid[row] + board[row]
def __expand_left(self):
board = Board.__generate_board(self.grid, True)
self.ant.position = Position(self.ant.position.col + len(board[0]) - 1, self.ant.position.row)
print(f"Expanding left {self.ant.position}")
for row in range(0, len(self.grid)):
self.grid[row] = board[row] + self.grid[row]
def __expand_top(self):
board = Board.__generate_board(self.grid, False)
self.ant.position = Position(col=self.ant.position.col, row=self.ant.position.row + len(board) - 1)
print(f"Expanding top {self.ant.position}")
for row in range(0, len(self.grid)):
board.append(self.grid[row])
self.grid = board
def __expand_down(self):
board = Board.__generate_board(self.grid, False)
print(f"Expanding down {self.ant.position}")
for row in range(0, len(board)):
self.grid.append(board[row])
def print(self):
for i in range(0, len(self.grid)):
print(' '.join(map(lambda x: str(x), self.grid[i])))
print('\n')
def langton_ant():
"""
An ant is sitting on an infinite grid of white and black squares. It initially faces right.
At each step, it does the following:
(1) At a white square, flip the color of the square, turn 90 degrees right (clockwise), and move forward one unit.
(2) At a black square, flip the color of the square, turn 90 degrees left (counter-clockwise), and move forward one unit.
Write a program to simulate the first K moves that the ant makes and print the final board as a grid.
"""
board = Board()
for i in range(1000):
board.move()
# board.print()
return board.grid
if __name__ == "__main__":
grid: List[List[Cell]] = langton_ant()
with open("langton_ant.html", "w") as f:
f.write('<html>')
f.write('''
<style>
.scroll {
margin: 4px, 4px;
padding: 4px;
width: 300px;
overflow-x: auto;
overflow-y: auto;
white-space: nowrap;
}
</style>
''')
f.write('<body class="scroll">')
for i in grid:
f.write('<div>')
f.write('<span>')
f.write(''.join(map(lambda x: x.as_html(), i)))
f.write('</span>')
f.write('</div>')
f.write('\n')
f.write('</body></html>')