-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmastermind.py
More file actions
57 lines (43 loc) · 1.81 KB
/
Copy pathmastermind.py
File metadata and controls
57 lines (43 loc) · 1.81 KB
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
# mastermind
from itertools import product
BLACK_RESPONSE = 1
WHITE_RESPONSE = 0
NO_RESPONSE = -1
# CODE = [int(elem) for elem in input("Give me a code separating each element by a -: ").split("-")]
CODE = [0,1,2,5]
LENGTH_OF_CODE = len(CODE)
NUMBER_OF_COLOURS = int(max(CODE)) + 1
def get_feedback(guess, solution):
whites = [WHITE_RESPONSE if guess_single in solution else NO_RESPONSE for guess_single in guess]
blacks_and_whites = [BLACK_RESPONSE if guess[i] == solution[i] else whites[i] for i in range(LENGTH_OF_CODE)]
return blacks_and_whites
def get_all_combinations():
return [list(combination) for combination in list(product(list(range(0, NUMBER_OF_COLOURS)), repeat=LENGTH_OF_CODE))]
def update_remaining_combinations(previous_guess, previous_feedback, remaining_combinations):
return list(filter(lambda combination: get_feedback(previous_guess, combination) == previous_feedback, remaining_combinations))
def solution(code=""):
if len(code):
CODE = code
LENGTH_OF_CODE = len(CODE)
NUMBER_OF_COLOURS = int(max(CODE)) + 1
print(f"CODE = {CODE}")
remaining_combinations = get_all_combinations()
guess = [0]*(LENGTH_OF_CODE//2) + [1]*(LENGTH_OF_CODE - LENGTH_OF_CODE//2)
remaining_combinations.remove(guess)
tries = 0
if guess == CODE:
return 1
while guess != CODE:
previous_feedback = get_feedback(guess, CODE)
tries += 1
remaining_combinations = update_remaining_combinations(guess, previous_feedback, remaining_combinations)
guess = remaining_combinations.pop(0)
print(f"Solved {guess} in {tries} attempts")
return tries
def benchmark():
attempts = []
for code in get_all_combinations():
attempts.append(solution(code))
print(sum(attempts))
print(benchmark())
print(NUMBER_OF_COLOURS)