-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem640.py
More file actions
67 lines (60 loc) · 1.94 KB
/
problem640.py
File metadata and controls
67 lines (60 loc) · 1.94 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
57
58
59
60
61
62
63
64
65
66
import random
numberSimulations = 100000000
numberCards = 12
def diceRoll():
return random.randint(1,6)
def game():
array = [0 for x in range(numberCards)]
arrayDone = False
turnCounter = 0
while not arrayDone:
turned = False
roll1 = diceRoll()
roll2 = diceRoll()
maximum = max(roll1, roll2)
minimum = min(roll1, roll2)
total = roll1 + roll2
# if total above 6, take it
if total > 6:
# if unturned
if array[total-1] == 0:
# turn it
array[total-1] = 1
# record that you turned it
turned = True
# if you haven't turned one yet
if not turned:
# if you can pick the lowest number
if array[minimum-1] == 0:
# turn it
array[minimum-1] = 1
# else pick highest single that you can (still lower than sum so better)
elif array[maximum-1] == 0:
# turn it
array[maximum-1] = 1
# else flip the sum (since highest chance if sum less than 6)
else:
# if total less than 6
if total < 6:
if array[total-1] == 0:
# turn it
array[total-1] = 1
# otherwise
else:
# turn it back
array[total-1] = 0
# otherwise turn back max
else:
array[maximum-1] = 0
arrayDone = True
for elem in array:
if elem == 0:
arrayDone = False
## print("Round %d, rolls = %d %d" % (turnCounter, roll1, roll2))
## print(array)
turnCounter = turnCounter + 1
return turnCounter
turns = []
for i in range(numberSimulations):
turns.append(game())
print(str(sum(turns)/numberSimulations))