Moscow Puzzles 89: Siskin and Thrush
Posted on Sat 30 October 2021 in tech • Tagged with programming, moscow puzzles, python
Moscow Puzzles #89
Siskin and Thrush
class birdcage: | |
""" | |
Moscow Puzzles 89 - Siskin and Thrush: | |
At the end of summer camp the children decided to free the 20 birds they caught. | |
Line up the cages in a row, counting from left to right, open each fifth cage with a bird in it. | |
You may take the last two birds to the city. The children wanted to take Siskin and Thrush. | |
Which cages should contain them? | |
""" | |
N = 20 | |
SKIP = 5 | |
def __init__(self): | |
self.cage = [False] * self.N | |
self.visited = set() | |
self.total = set() | |
for i in range(1, self.N+1): | |
self.total.add(i) | |
def iterate(self, pos): | |
pos += 1 | |
if pos == self.N+1: | |
pos = 1 | |
while pos in self.visited: | |
pos += 1 | |
if pos == self.N+1: | |
pos = 1 | |
return pos | |
def skip_count(self): | |
pos = 0 | |
skcount = 0 | |
while len(self.visited) < self.N - 2: | |
if skcount < self.SKIP: | |
skcount += 1 | |
pos = self.iterate(pos) | |
else: | |
self.visited.add(pos) | |
skcount = 0 | |
return self.total - self.visited; | |
if __name__ == '__main__': | |
cage = birdcage() | |
closed = cage.skip_count() | |
print(closed) |