-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.py
154 lines (130 loc) · 3.84 KB
/
21.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import re
import pyperclip
from collections import defaultdict, deque
import tqdm
import math
import heapq
from itertools import permutations, product
import numpy as np
def process(filename):
f = open(filename, 'r')
content = f.read()
return content.splitlines()
def get_blocks(lst):
blocks = []
block = []
for x in lst:
if len(x) == 0:
blocks.append(block)
block = []
else:
block.append(x)
blocks.append(block)
return blocks
dice = 0
def roll():
global dice
dice = (dice % 100) + 1
return dice
def neighbors(p1_score, p2_score, p1_pos, p2_pos, turn, track):
lst = []
for r, cnt in [(3, 1), (4, 3), (5, 6), (6, 7), (7, 6), (8, 3), (9, 1)]:
if turn == 0:
old_p2_score = p2_score - track[p2_pos]
old_p2_pos = (p2_pos - r) % len(track)
lst.append((p1_score, old_p2_score, p1_pos, old_p2_pos, 1 - turn, cnt))
else:
old_p1_score = p1_score - track[p1_pos]
old_p1_pos = (p1_pos - r) % len(track)
lst.append((old_p1_score, p2_score, old_p1_pos, p2_pos, 1 - turn, cnt))
return lst
def combs():
s = defaultdict(int)
for i in range(1, 4):
for j in range(1,4):
for k in range(1, 4):
s[i + j + k] += 1
return [(k, s[k]) for k in s]
def num(state, start, track):
if state == start:
return 1
p1_score, p2_score, p1_pos, p2_pos, turn = state
return sum([num((p1_s, p2_s, p1_p, p2_p, t), start, track) * cnt for p1_s, p2_s, p1_p, p2_p, t, cnt in neighbors(p1_score, p2_score, p1_pos, p2_pos, turn, track)])
def solve(lst):
result = 0
scores = [0, 0]
players = [7, 6]
turn = 0
track = [i + 1 for i in range(10)]
# cnt = 0
# while max(scores) < 1000:
# r = sum([roll() for _ in range(3)])
# print(r)
# players[turn] = (players[turn] + r) % len(track)
# scores[turn] += track[players[turn]]
# turn = 1 - turn
# cnt += 3
print(track)
# print(scores, cnt)
# return cnt * min(scores)
m = defaultdict(int) # player 1 score, player 2 score, player 1 pos, player 2 pos, turn
start = (0, 0, 7, 6, 0)
m[start] = 1
end = 21
for p2_score in range(end + 10):
for p1_score in range(1, end + 10):
for p1_pos in range(10):
for p2_pos in range(10):
for turn in [0, 1]:
total = 0
for p1_s, p2_s, p1_p, p2_p, t, cnt in neighbors(p1_score, p2_score, p1_pos, p2_pos, turn, track):
if p1_s >= end or p2_s >= end:
continue
# elif p1_s < 0 or p2_s < 0:
# continue
else:
total += cnt * m[p1_s, p2_s, p1_p, p2_p, t]
m[(p1_score, p2_score, p1_pos, p2_pos, turn)] = total
# m[(p1_score, p2_score, p1_pos, p2_pos, turn)] = sum([ * cnt for p1_s, p2_s, p1_p, p2_p, t, cnt in neighbors(p1_score, p2_score, p1_pos, p2_pos, turn, track)])
# print(neighbors(12, 13, 2, 3, 0, track))
# seen = set()
# fringe = [(0, 0, 7, 6, 0)]
# m[(0, 0, 7, 6, 0)] = 1
# while fringe:
# x = fringe.pop()
# p1_score, p2_score, p1_pos, p2_pos, turn = x
# # if x in seen:
# # continue
# if p1_score >= 21 or p2_score >= 21:
# continue
# # seen.add(x)
# for r, cnt in [(3, 1), (4, 3), (5, 6), (6, 7), (7, 6), (8, 3), (9, 1)]:
# if turn == 1:
# new_p2_pos = (p2_pos + r) % len(track)
# new_p2_score = p2_score + track[new_p2_pos]
# y = (p1_score, new_p2_score, p1_pos, new_p2_pos, 1 - turn)
# else:
# new_p1_pos = (p1_pos + r) % len(track)
# new_p1_score = p1_score + track[new_p1_pos]
# y = (new_p1_score, p2_score, new_p1_pos, p2_pos, 1 - turn)
# fringe.append(y)
# m[y] += m[x] * cnt
# print(set(m.values()))
scores = [0, 0]
for p1_score, p2_score, p1_pos, p2_pos, turn in m:
k = m[(p1_score, p2_score, p1_pos, p2_pos, turn)]
if p1_score >= end and p2_score >= end:
continue
if p1_score >= end:
if k:
print((p1_score, p2_score, p1_pos, p2_pos, turn), k)
scores[0] += k
if p2_score >= end:
scores[1] += k
print(scores)
return max(scores)
cache = dict()
lst = process('input')
x = solve(lst)
print(x)
pyperclip.copy(x)