-
Notifications
You must be signed in to change notification settings - Fork 0
/
id0084.c
172 lines (142 loc) · 3.94 KB
/
id0084.c
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
// Licensed under the MIT License.
// Monopoly Odds
#include "../lib/euler.h"
#include "../lib/priority_queue.h"
#include "../lib/random.h"
enum Square
{
SQUARE_GO = 0,
SQUARE_COMMUNITY_CHEST_1 = 2,
SQUARE_CHANCE_1 = 7,
SQUARE_JAIL = 10,
SQUARE_ST_CHARLES_PLACE = 11,
SQUARE_COMMUNITY_CHEST_2 = 17,
SQUARE_CHANCE_2 = 22,
SQUARE_ILLINOIS_AVENUE = 24,
SQUARE_GO_TO_JAIL = 30,
SQUARE_COMMUNITY_CHEST_3 = 33,
SQUARE_CHANCE_3 = 36,
SQUARE_BOARDWALK = 39,
MAX_SQUARE = 40
};
struct Monopoly
{
long long (*random)(void* instance, long long max);
void* randomState;
enum Square square;
int doubles;
int dieFaces;
};
typedef enum Square Square;
typedef struct Monopoly* Monopoly;
static Square square_next(Square square, Square others[])
{
for (Square* other = others; *other; other++)
{
if (square < *other)
{
return *other;
}
}
return others[0];
}
void monopoly(Monopoly instance, Random random, Object seed, int dieFaces)
{
instance->random = random;
instance->randomState = seed;
instance->square = SQUARE_GO;
instance->doubles = 0;
instance->dieFaces = dieFaces;
}
static int monopoly_random(Monopoly instance, int max)
{
return instance->random(instance->randomState, max);
}
Square monopoly_next_square(Monopoly instance)
{
int a = monopoly_random(instance, instance->dieFaces) + 1;
int b = monopoly_random(instance, instance->dieFaces) + 1;
if (a == b)
{
instance->doubles++;
if (instance->doubles == 3)
{
return SQUARE_JAIL;
}
}
else
{
instance->doubles = 0;
}
Square square = (instance->square + a + b) % MAX_SQUARE;
Square utilitySquares[] = { 12, 28, 0 };
Square railroadSquares[] = { 5, 15, 25, 35, 0 };
switch (square)
{
case SQUARE_GO_TO_JAIL:
instance->doubles = 0;
return SQUARE_JAIL;
case SQUARE_CHANCE_1:
case SQUARE_CHANCE_2:
case SQUARE_CHANCE_3:
switch (monopoly_random(instance, 16))
{
case 0: return SQUARE_GO;
case 1: return SQUARE_JAIL;
case 2: return SQUARE_ST_CHARLES_PLACE;
case 3: return SQUARE_ILLINOIS_AVENUE;
case 4: return SQUARE_BOARDWALK;
case 5: return railroadSquares[0];
case 6:
case 7:
return square_next(square, railroadSquares);
case 8: return square_next(square, utilitySquares);
case 9: return square - 3;
}
break;
case SQUARE_COMMUNITY_CHEST_1:
case SQUARE_COMMUNITY_CHEST_2:
case SQUARE_COMMUNITY_CHEST_3:
switch (monopoly_random(instance, 16))
{
case 0: return SQUARE_GO;
case 1: return SQUARE_JAIL;
}
break;
default: break;
}
return square;
}
int main(void)
{
long result = 0;
struct Monopoly game;
struct PriorityQueue priorityQueue;
int counts[MAX_SQUARE] = { 0 };
uint64_t seed[4] = { 12004, 1, 20, 2004 };
clock_t start = clock();
monopoly(&game, xoshiro256_star_star_random, seed, 4);
for (long i = 0; i < 100000l; i++)
{
game.square = monopoly_next_square(&game);
counts[game.square]++;
}
euler_ok(priority_queue(
&priorityQueue,
sizeof(Square),
sizeof(int),
MAX_SQUARE,
reverse_int_comparer));
for (Square square = 0; square < MAX_SQUARE; square++)
{
priority_queue_enqueue(&priorityQueue, &square, counts + square);
}
for (int i = 0; i < 3; i++)
{
Square square;
priority_queue_try_dequeue(&priorityQueue, &square, NULL);
result = result * 100 + square;
}
finalize_priority_queue(&priorityQueue);
return euler_submit(84, result, start);
}