Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- namespace DailyProgrammer259Hard
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- for (int i = 1; i <= 500; i++)
- {
- Console.WriteLine($"{i} --> {DecToNos(i)}");
- }
- Console.WriteLine(sw.ElapsedMilliseconds);
- Console.ReadLine();
- }
- public static int NosToDec(string nos)
- {
- int dec = 0;
- for (int i = 0; i < nos.Length; i++)
- {
- switch (nos[i])
- {
- case '0':
- dec += i + 1;
- break;
- case '1':
- dec -= i + 1;
- break;
- case '2':
- dec *= i + 1;
- break;
- }
- }
- return dec;
- }
- public static string DecToNos(int dec)
- {
- // Find shortest solutions
- List<string> solutions = new List<string>();
- string guess = "0";
- while (true)
- {
- if (NosToDec(guess) == dec)
- {
- solutions.Add(guess);
- }
- string guess2 = IterateNosString(guess);
- if (guess2.Length != guess.Length && solutions.Count > 0)
- {
- break;
- }
- guess = guess2;
- }
- // Tiebreaker #1: Most zeros
- List<string> tiebreaker1 = new List<string>();
- int peakZeros = -1;
- foreach (string solution in solutions)
- {
- int zeros = solution.Count(c => c == '0');
- if (zeros == peakZeros)
- {
- tiebreaker1.Add(solution);
- }
- else if (zeros > peakZeros)
- {
- tiebreaker1.Clear();
- tiebreaker1.Add(solution);
- peakZeros = zeros;
- }
- }
- // Tiebreaker #2: Lowest
- string winner = tiebreaker1[0];
- foreach(string solution in tiebreaker1)
- {
- if (String.CompareOrdinal(solution, winner) < 0)
- {
- winner = solution;
- }
- }
- // Return
- return winner;
- }
- private static string IterateNosString(string nos)
- {
- StringBuilder sb = new StringBuilder(nos);
- for (int i = nos.Length - 1; i >= 0; i--)
- {
- char c = sb[i];
- if (c == '0')
- {
- sb[i] = '1';
- break;
- }
- if (c == '1')
- {
- sb[i] = '2';
- break;
- }
- if (c == '2')
- {
- sb[i] = '0';
- if (i == 0)
- {
- sb.Insert(0, '0');
- }
- }
- }
- return sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement