-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglass.py
37 lines (29 loc) · 972 Bytes
/
glass.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Given n glasses standing upright at the beginning.
In one step any k of the glasses need to be turned over.
Our aim is to reach the state in which all glasses are standing downright.
"""
from search import Problem, breadth_first_graph_search
from itertools import combinations
class Glasses(Problem):
"Turn glasses"
def __init__(self, n, k):
self.initial = tuple([False]*n)
self.goal = tuple([True]*n)
self.turn = k
def actions(self, state):
return combinations(range(len(state)), self.turn)
def result(self, state, action):
glasses = list(state)
turn = list(action)
for i in turn:
glasses[i] = not glasses[i]
return tuple(glasses)
def value(self, state):
"""we have no good heuristics"""
return 0
if __name__ == "__main__":
g = Glasses(6,2)
print(breadth_first_graph_search(g).solution())