-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections_helper.py
63 lines (52 loc) · 2.21 KB
/
connections_helper.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from itertools import permutations
from typing import Set, Tuple
words = ["mood", "volleyball", "hockey", "table", "breaking", "trampoline", "skeleton", "record"]
# Past guesses, and if they resulted in a "One away" message or not
hints = {
("mood", "breaking", "skeleton", "record"): False,
("mood", "volleyball", "trampoline", "skeleton"): False,
('hockey', 'mood', 'record', 'table'): True,
}
Groups = Tuple[Tuple[str, str, str, str]]
Groupings = Set[Groups]
def make_possible_groupings() -> Groupings:
possible_groupings = set()
for p in permutations(words):
groups = []
# Break permutation up into groups of 4. Sort and add to set to remove duplicates.
# Example: [(a, b, c, d), (e, f, g, h)] is a duplicate of [(e, f, g, h), (a, b, c, d)]
for i in range(0, len(p), 4):
groups.append(tuple(sorted(p[i:i+4])))
possible_groupings.add(tuple(sorted(groups)))
print(len(possible_groupings))
return possible_groupings
def apply_hints(possible_groupings: Groupings) -> Groupings:
filtered_groupings = possible_groupings
for guess, clue in hints.items():
new_set = set()
print(guess, clue)
for grouping in filtered_groupings:
num_groupings = len(grouping)
counts = [0] * num_groupings
for word in guess:
# Check which grouping the word is in and increment that count
for i in range(num_groupings):
if word in grouping[i]:
counts[i] += 1
break
if clue and 3 in counts:
# The clue was "One away" and one of the groups had 3 of the guessed words. Good guess.
new_set.add(grouping)
elif not clue and 3 not in counts:
# The clue was not "One away" and none of the groups had 3 of the guessed words. Good guess.
new_set.add(grouping)
filtered_groupings = new_set
print(len(filtered_groupings))
return filtered_groupings
def main():
possible_groupings = make_possible_groupings()
groups = apply_hints(possible_groupings)
for group in groups:
print(group)
if __name__ == '__main__':
main()