-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
207 lines (179 loc) · 5.11 KB
/
game.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from dataclasses import dataclass, field, replace
from random import randint
from collections.abc import Callable
from functools import cached_property
from statistics import mean
import sys
def d6():
return randint(1, 6)
def drinkroll_min():
return min(d6(), d6())
def throw_negative():
return d6()
def newroll_min() -> (int, bool):
roll1 = d6()
roll2 = d6()
roll = min(roll1, roll2)
is_double = roll1 == roll2
return roll, is_double
@dataclass
class Drink:
abv: float
vol: float
@cached_property
def ethanol(self):
return self.abv * 0.01 * self.vol
drinks: dict[str, Drink] = {}
for l in open("drinks.txt"):
l = l.strip()
if not l or l.startswith("//"):
continue
name, content = l.split(" ")
bits = content.split(",")
while bits:
abv, vol, carb = bits[:3]
bits = bits[3:]
abv = float(abv.removesuffix("%"))
vol = float(vol)
drinks[name] = Drink(abv, vol)
PORTION = drinks["beer"].ethanol
@dataclass
class Square:
name: str
drinks: Callable[[int], list[Drink]] = field(default_factory=list)
refill: bool = False
teleport: str | None = None
sqtypes: dict[str, Square] = {}
place_file = sys.argv[1]
for l in open(place_file):
l = l.strip()
if not l or l.startswith("//"):
continue
name, *bits = l.split(" ")
assert name not in sqtypes
drinkfuncs = []
refill = False
while bits:
bit = bits.pop(0)
match bit:
case "refill":
refill = True
case _:
drinkfuncs.append(
eval(f"lambda drink: lambda nvisits: [drink] * {bits.pop(0)}")(
drinks[bit]
)
)
sqtypes[name] = Square(
name,
(
lambda drinkfuncs: lambda nvisits: sum(
(f(nvisits=nvisits) for f in drinkfuncs),
[],
)
)(drinkfuncs),
refill,
)
squares: list[Square] = []
sqnames: dict[str, int] = {}
board_file = sys.argv[2]
for l in open(board_file):
l = l.strip()
if not l or l.startswith("//"):
continue
bits = l.split(" ")
if bits[0].isdigit():
mult = int(bits.pop(0))
else:
mult = 1
tname = bits.pop(0)
sq = replace(sqtypes[tname])
for bit in bits:
match bit[0]:
case "#":
sqnames[bit[1:]] = len(squares)
case ">":
sq.teleport = bit[1:]
case _:
raise ValueError(bit)
for _ in range(mult):
squares.append(sq)
endpos = next(i for i, sq in enumerate(squares) if sq.name == "end")
@dataclass
class Result:
rounds: int
turns: int
top_portions: float
avg_portions: float
top_drinks: int
avg_drinks: float
visits: list[int]
sq_drinks: list[int]
sq_portions: list[float]
def game(nplayers: int):
drunk = [False] * len(squares)
visits = [0] * len(squares)
sq_drinks = [0] * len(squares)
sq_portions = [0] * len(squares)
players = [0] * nplayers
drinks = [0] * nplayers
portions = [0.0] * nplayers
rounds = 0
turns = 0
done = False
while not done:
rounds += 1
for p in range(nplayers):
turns += 1
move, is_double = newroll_min()
pos = players[p]
if pos < endpos and pos + move > endpos:
extra = pos + move - endpos
pos = endpos - extra
elif pos + move >= len(squares):
pos = len(squares) - 1
else:
pos += move
players[p] = pos
visits[pos] += 1
sq = squares[pos]
if not drunk[pos]:
if sq.name == "keto":
pos -= len(sq.drinks(nvisits=visits[pos]))
players[p] = pos
visits[pos] += 1
sq = squares[pos]
to_drink: list[Drink] = sq.drinks(nvisits=visits[pos])
if is_double:
now_drinks = len(to_drink)
now_portions = sum(d.ethanol for d in to_drink) / PORTION
drinks[p] += now_drinks
portions[p] += now_portions
sq_drinks[pos] += now_drinks
sq_portions[pos] += now_portions
now_drinks = len(to_drink)
now_portions = sum(d.ethanol for d in to_drink) / PORTION
drinks[p] += now_drinks
portions[p] += now_portions
sq_drinks[pos] += now_drinks
sq_portions[pos] += now_portions
if not sq.refill:
drunk[pos] = True
if sq.teleport:
pos = sqnames[sq.teleport]
players[p] = pos
visits[pos] += 1
if sq.name == "end":
done = True
break
return Result(
rounds,
turns,
max(portions),
mean(portions),
max(drinks),
mean(drinks),
visits,
sq_drinks,
sq_portions,
)