1
0
Fork 0

Problem 84 - refactored, still broken

This commit is contained in:
Jonathan Chan 2018-08-23 09:51:57 -07:00
parent 7f9c636489
commit 9f101e40d4
2 changed files with 64 additions and 86 deletions

BIN
84 Normal file → Executable file

Binary file not shown.

148
84.c
View File

@ -1,113 +1,91 @@
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define GO 0
#define JAIL 10
#define G2J 30
const int GO = 0, JAIL = 10, G2J = 30; // GO/JAIL
const int CC1 = 2, CC2 = 17, CC3 = 33; // Community Chest
const int CH1 = 7, CH2 = 22, CH3 = 36; // Chance
const int R1 = 5, R2 = 15, R3 = 25; // Railway
const int U1 = 12, U2 = 28; // Utilities
const int C1 = 11, E3 = 24, H2 = 39; // other
#define CC1 2
#define CC2 17
#define CC3 33
#define CH1 7
#define CH2 22
#define CH3 36
#define C1 11
#define E3 24
#define H2 39
#define R1 5
#define R2 15
#define R3 25
#define U1 12
#define U2 28
int CC[2] = {GO, JAIL};
int CH[6] = {GO, JAIL, C1, E3, H2, R1};
unsigned long long freq[40] = {0};
int freq[40] = {0};
int doubleRolls = 0;
int position = GO;
int communityChestIndex = 0;
int chanceIndex = 0;
int cci = 0;
int chi = 0;
void moveToSquare(int square) {
position = square;
freq[square]++;
}
void communityChest() {
communityChestIndex = ++communityChestIndex % 16;
if (communityChestIndex == 1 || communityChestIndex == 2) {
moveToSquare(CC[communityChestIndex - 1]);
} else {
moveToSquare(position);
int chance() {
chi = (chi + 1) % 16;
switch (chi) {
case 1: return GO;
case 2: return JAIL;
case 3: return C1;
case 4: return E3;
case 5: return H2;
case 6: return R1;
case 7:
case 8:
if (position == CH1) return R2;
if (position == CH2) return R3;
return R1;
case 9:
if (position == CH2) return U2;
return U1;
case 10: position -= 3;
default: return position;
}
}
void chance() {
chanceIndex = ++chanceIndex % 16;
if (chanceIndex >= 1 && chanceIndex <= 6) {
moveToSquare(CC[chanceIndex - 1]);
} else if (chanceIndex == 7 || chanceIndex == 8) {
if (position == CH1) {
moveToSquare(R2);
} else if (position == CH2) {
moveToSquare(R3);
} else {
moveToSquare(R1);
}
} else if (chanceIndex == 9) {
if (position == CH2) {
moveToSquare(U2);
} else {
moveToSquare(U1);
}
} else if (chanceIndex == 10) {
position -= 3;
if (position == CH3) {
communityChest();
} else {
moveToSquare(position);
}
} else {
moveToSquare(position);
int communityChest() {
cci = (cci + 1) % 16;
switch (cci) {
case 1: return GO;
case 2: return JAIL;
default: return position;
}
}
int main(void) {
srand(time(NULL));
for (unsigned long long i = 0; i < 20000000; i++) {
int die1 = (rand() % 4) + 1;
int die2 = (rand() % 4) + 1;
int dice = die1 + die2;
for (int i = 0; i < 40000000; i++) {
int die1 = (rand() % 6) + 1;
int die2 = (rand() % 6) + 1;
position = (position + die1 + die2) % 40;
if (die1 == die2) {
doubleRolls = ++doubleRolls % 3;
doubleRolls = (doubleRolls + 1) % 3;
if (doubleRolls == 0) {
moveToSquare(JAIL);
continue;
position = JAIL;
}
}
position = (position + dice) % 40;
if (position == G2J) {
moveToSquare(JAIL);
} else if (position == CC1 || position == CC2 || position == CC3) {
communityChest();
} else if (position == CH1 || position == CH2 || position == CH3) {
chance();
} else {
moveToSquare(position);
position = JAIL;
}
if (position == CH1 || position == CH2 || position == CH3) {
position = chance();
}
if (position == CC1 || position == CC2 || position == CC3) {
position = communityChest();
}
freq[position]++;
}
float sum = 0;
for (int i = 0; i < 40; i++) {
printf("%02d: %llu\n", i, freq[i]);
sum += freq[i];
}
for (int i = 1; i <= 3; i++) {
int topSquare = 0, topValue = 0;
float topFreq = 0;
for (int j = 0; j < 40; j++) {
if (freq[j] > topValue) {
topSquare = j;
topValue = freq[j];
topFreq = topValue / sum;
freq[j] = 0;
}
}
printf("#%d: %d @ %f%%\n", i, topSquare, topFreq * 100);
}
}