Advertisement
isoZachary

Daily Programmer 259 Hard

Apr 20th, 2016
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace DailyProgrammer259Hard
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Stopwatch sw = new Stopwatch();
  14.             sw.Start();
  15.             for (int i = 1; i <= 500; i++)
  16.             {
  17.                 Console.WriteLine($"{i} --> {DecToNos(i)}");
  18.             }
  19.             Console.WriteLine(sw.ElapsedMilliseconds);
  20.  
  21.             Console.ReadLine();
  22.         }
  23.  
  24.         public static int NosToDec(string nos)
  25.         {
  26.             int dec = 0;
  27.  
  28.             for (int i = 0; i < nos.Length; i++)
  29.             {
  30.                 switch (nos[i])
  31.                 {
  32.                     case '0':
  33.                         dec += i + 1;
  34.                         break;
  35.                     case '1':
  36.                         dec -= i + 1;
  37.                         break;
  38.                     case '2':
  39.                         dec *= i + 1;
  40.                         break;
  41.                 }
  42.             }
  43.  
  44.             return dec;
  45.         }
  46.  
  47.         public static string DecToNos(int dec)
  48.         {
  49.             // Find shortest solutions
  50.             List<string> solutions = new List<string>();
  51.             string guess = "0";
  52.             while (true)
  53.             {
  54.                 if (NosToDec(guess) == dec)
  55.                 {
  56.                     solutions.Add(guess);
  57.                 }
  58.  
  59.                 string guess2 = IterateNosString(guess);
  60.                 if (guess2.Length != guess.Length && solutions.Count > 0)
  61.                 {
  62.                     break;
  63.                 }
  64.                 guess = guess2;
  65.             }
  66.  
  67.             // Tiebreaker #1: Most zeros
  68.             List<string> tiebreaker1 = new List<string>();
  69.             int peakZeros = -1;
  70.  
  71.             foreach (string solution in solutions)
  72.             {
  73.                 int zeros = solution.Count(c => c == '0');
  74.  
  75.                 if (zeros == peakZeros)
  76.                 {
  77.                     tiebreaker1.Add(solution);
  78.                 }
  79.                 else if (zeros > peakZeros)
  80.                 {
  81.                     tiebreaker1.Clear();
  82.                     tiebreaker1.Add(solution);
  83.                     peakZeros = zeros;
  84.                 }
  85.             }
  86.  
  87.             // Tiebreaker #2: Lowest
  88.             string winner = tiebreaker1[0];
  89.  
  90.             foreach(string solution in tiebreaker1)
  91.             {
  92.                 if (String.CompareOrdinal(solution, winner) < 0)
  93.                 {
  94.                     winner = solution;
  95.                 }
  96.             }
  97.  
  98.             // Return
  99.             return winner;
  100.         }
  101.  
  102.         private static string IterateNosString(string nos)
  103.         {
  104.             StringBuilder sb = new StringBuilder(nos);
  105.  
  106.             for (int i = nos.Length - 1; i >= 0; i--)
  107.             {
  108.                 char c = sb[i];
  109.                 if (c == '0')
  110.                 {
  111.                     sb[i] = '1';
  112.                     break;
  113.                 }
  114.                 if (c == '1')
  115.                 {
  116.                     sb[i] = '2';
  117.                     break;
  118.                 }
  119.                 if (c == '2')
  120.                 {
  121.                     sb[i] = '0';
  122.                     if (i == 0)
  123.                     {
  124.                         sb.Insert(0, '0');
  125.                     }
  126.                 }
  127.             }
  128.  
  129.             return sb.ToString();
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement