DeepSeek R1 のコーディング能力を試す 2
以前の投稿でDeepSeek R1でWeasel Programを試してみたところChatGPTとは大きく異なる挙動が見られた.そこで今回はDeepSeek R1でLangton’s Antを試してみたい.
やはり挙動が大きく異なる.
Langton's AntのシミュレーションをPythonで実装.
エラーが出ないように自分で点検して.
from collections import defaultdict
class LangtonsAnt:
def __init__(self):
self.grid = defaultdict(int)
self.position = (0, 0)
self.directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 北・東・南・西
self.current_dir = 0 # 初期方向:北
self.color_map = {0: '□', 1: '■'}
def rotate(self, color):
"""色に応じた方向転換ロジック"""
self.current_dir = (self.current_dir + (1 if color == 0 else -1)) % 4
def move(self):
"""1ステップの移動処理"""
x, y = self.position
current_color = self.grid[(x, y)]
# 回転と色反転
self.rotate(current_color)
self.grid[(x, y)] ^= 1 # 0/1切り替え
# 新しい位置へ移動
dx, dy = self.directions[self.current_dir]
self.position = (x + dx, y + dy)
def visualize(self, size=15):
"""現在のグリッド状態を可視化"""
cx, cy = self.position
half = size // 2
return '\n'.join(
' '.join(
f'>{self.color_map[self.grid[(x, y)]]}<' if (x, y) == self.position
else self.color_map[self.grid[(x, y)]]
for x in range(cx - half, cx + half + 1)
)
for y in range(cy + half, cy - half - 1, -1)
)
# 実行例
if __name__ == "__main__":
ant = LangtonsAnt()
for _ in range(100):
ant.move()
print("100 steps:")
print(ant.visualize(11))
Output 例
100 steps:
□ □ □ □ □ □ □ □ □ □ □
□ □ □ □ □ □ □ □ □ □ □
□ □ □ □ □ □ □ □ □ □ □
□ □ □ □ □ ■ ■ □ □ □ □
□ □ □ □ ■ □ □ ■ □ □ □
□ □ □ □ □ >■< □ □ ■ □ □
□ □ □ □ ■ □ □ □ ■ □ □
□ □ □ ■ ■ ■ □ ■ □ □ □
□ □ ■ □ □ ■ □ □ □ □ □
□ □ ■ □ □ ■ □ □ □ □ □
□ □ □ ■ □ □ ■ □ □ □ □