Skip to content

Instantly share code, notes, and snippets.

@di

di/oneeyedrobot.py

Created May 6, 2013
Embed
What would you like to do?
JGC's "one-eyed robot" problem, implemented in Python. See http://dustingram.com/articles/2013/05/02/z-machines-and-one-eyed-robots/
#!/usr/bin/python
import random
class ball:
def __init__(self, color):
self.color = color
self.examined = 0
def look(self):
self.examined += 1
return self.color
class buckets:
def __init__(self, size=15):
self.line = [ball(random.choice(['r', 'g', 'b'])) for _ in range(size)]
self.i = 0
self.r = 0
self.b = size-1
def swap(self, i, j):
self.line[i], self.line[j] = self.line[j], self.line[i]
def check(self):
c = self.line[self.i].look()
if c == 'r':
self.swap(self.i, self.r)
self.r += 1
self.i += 1
elif c == 'b':
self.swap(self.i, self.b)
self.b -= 1
else:
self.i += 1
def solve(self):
while self.i <= self.b:
self.check()
b = buckets()
print([x.color for x in b.line])
b.solve()
print([x.color for x in b.line])
print([x.examined for x in b.line])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.