Lawl~~~

Project Euler problem 84~
c++

  1:     class Program
  2:     {
  3:         static void Main(string[] args)
  4:         {
  5:             Console.WriteLine("The six-digit modal string: ");
  6: 
  7:             TestBenchSetup();
  8:             TestBenchLoader(plain_simulation_with_one_mil_rolls);
  9: 
 10:             Console.WriteLine("press any key to exit");
 11:             Console.ReadKey();
 12:         }
 13: 
 14:         static string plain_simulation_with_one_mil_rolls()
 15:         {
 16:             var board = new double[40];
 17:             var count = new int[40];
 18: 
 19:             // -1: nothing
 20:             var chest = new int[] { 0, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
 21:             // -88: railroad -99: utility -3: go back three
 22:             var chance = new int[] { 0, 10, 11, 24, 39, 5, -88, -88, -99, -3, -1, -1, -1, -1, -1, -1 };
 23: 
 24:             var dice1 = 0;
 25:             var dice2 = 0;
 26:             var rnd = new Random();
 27:             var current = 0;
 28: 
 29:             Shuffle(chest, rnd);
 30:             Shuffle(chance, rnd);
 31: 
 32:             var moves = 1000000;
 33:             var double_counter = 0;
 34:             var dice_side = 4;
 35: 
 36:             for (int i = 0; i < moves; i++)
 37:             {
 38:                 dice1 = rnd.Next(dice_side) + 1;
 39:                 dice2 = rnd.Next(dice_side) + 1;
 40:                 current += (dice1 + dice2);
 41: 
 42:                 if (current > 39)
 43:                     current -= 40;
 44: 
 45:                 if (dice1 == dice2)
 46:                     double_counter++;
 47:                 else
 48:                     double_counter = 0;
 49: 
 50:                 if (double_counter == 3 || current == 30)
 51:                     current = 10;
 52: 
 53:                 // chest
 54:                 if (current == 2 || current == 17 || current == 33)
 55:                 {
 56:                     var card = chest[rnd.Next(16)];
 57:                     if (card != -1)
 58:                         current = card;
 59:                 }
 60: 
 61:                 // chance
 62:                 if (current == 7 || current == 22 || current == 36)
 63:                 {
 64:                     var card = chance[rnd.Next(16)];
 65:                     if (card != -1)
 66:                     {
 67:                         switch (card)
 68:                         {
 69:                             case -88:
 70:                                 current = NextRailWay(current);
 71:                                 break;
 72:                             case -99:
 73:                                 current = NextUtility(current);
 74:                                 break;
 75:                             case -3:
 76:                                 current -= 3;
 77:                                 if (current == 33)
 78:                                 {
 79:                                     card = chest[rnd.Next(16)];
 80:                                     if (card != -1)
 81:                                         current = card;
 82:                                 }
 83:                                 break;
 84:                             default:
 85:                                 current = card;
 86:                                 break;
 87:                         };
 88:                     }
 89:                 }
 90: 
 91:                 if (current == 10)
 92:                     double_counter = 0;
 93: 
 94:                 count[current]++;
 95:             }
 96: 
 97:             return count.Select((p, i) => new { index = i, count = p }).OrderByDescending(p => p.count).Take(3)
 98:                 .Aggregate(new StringBuilder(), (s, n) => s.Append(n.index == 0 ? "00" : n.index.ToString())).ToString();
 99:         }
100: 
101:         static void Shuffle(int[] deck, Random rnd)
102:         {
103:             for (int i = deck.Length; i > 1; i--)
104:             {
105:                 var j = rnd.Next(i);
106:                 var tmp = deck[j];
107:                 deck[j] = deck[i - 1];
108:                 deck[i - 1] = tmp;
109:             }
110:         }
111: 
112:         static int DrawCard(int[] deck)
113:         {
114:             var card = deck[0];
115:             for (int i = 1; i < deck.Length; i++)
116:             {
117:                 deck[i - 1] = deck[i];
118:             }
119:             deck[deck.Length - 1] = card;
120:             return card;
121:         }
122: 
123:         static int NextRailWay(int current)
124:         {
125:             if (current == 7)
126:                 return 15;
127:             else if (current == 22)
128:                 return 25;
129:             else // current == 36
130:                 return 5;
131:         }
132: 
133:         static int NextUtility(int current)
134:         {
135:             if (current == 22)
136:                 return 28;
137:             else
138:                 return 12;
139:         }
140: 
141:         static Stopwatch stopwatch = new Stopwatch();
142:         static void TestBenchSetup()
143:         {
144:             // Uses the second Core or Processor for the Test
145:             Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
146:             // Prevents "Normal" processes from interrupting Threads
147:             Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
148:             // Prevents "Normal" Threads from interrupting this thread
149:             Thread.CurrentThread.Priority = ThreadPriority.Highest;
150:         }
151:         // see http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx
152:         static void TestBenchLoader(Func<string> test_method)
153:         {
154:             stopwatch.Reset();
155:             stopwatch.Start();
156:             string result = string.Empty;
157:             long avg_tick = 0;
158:             long avg_ms = 0;
159:             while (stopwatch.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 ms
160:             // stabilizes the CPU cache and pipeline.
161:             {
162:                 result = test_method(); // Warmup
163:             }
164:             stopwatch.Stop();
165:             for (int repeat = 0; repeat < 20; ++repeat)
166:             {
167:                 stopwatch.Reset();
168:                 stopwatch.Start();
169:                 result = test_method();
170:                 stopwatch.Stop();
171:                 avg_tick += stopwatch.ElapsedTicks;
172:                 avg_ms += stopwatch.ElapsedMilliseconds;
173:             }
174:             avg_tick = avg_tick / 20;
175:             avg_ms = avg_ms / 20;
176:             Console.WriteLine(string.Format("{0} way(ticks:{1}, ms:{2}) Ans:{3}",
177:                 test_method.Method.Name.Replace('_', ' '), avg_tick, avg_ms, result));
178:         }
179:     }

c#

Advertisements
This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s