using System.Collections.Generic;
static Dictionary<int, string> Digits
public static void Main(string[] args)
Digits = new Dictionary<int, string>{{0, "xxx.xxx"}, {1, "..x..x."}, {2, "x.xxx.x"}, {3, "x.xx.xx"}, {4, ".xxx.x."}, {5, "xx.x.xx"}, {6, "xx.xxxx"}, {7, "x.x..x."}, {8, "xxxxxxx"}, {9, "xxxx.xx"}, };
var allLCDs = new List<string>();
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
for (int k = 0; k < 10; k++)
var result = i * (10 * j + k);
var equation = new[]{i, j, k, a, b, c};
var lcd = EquationToLCD(equation);
var solution = Solve(allLCDs, 0, 0, int.MaxValue, "");
Console.WriteLine(solution);
Console.WriteLine(solutionStr);
static string solutionStr = null;
private static int Solve(List<string> allLCDs, int lineNum, int shown, int best, string str)
if (solutionStr.Length < allLCDs[0].Length)
solutionStr += new string ('?', allLCDs[0].Length - solutionStr.Length);
if (shown >= best || lineNum == allLCDs[0].Length)
var whites = allLCDs.Where(x => x[lineNum] != 'x').ToList();
var blacks = allLCDs.Where(x => x[lineNum] == 'x').ToList();
if (whites.Count > 0 && blacks.Count > 0)
best = Math.Min(Solve(whites, lineNum + 1, shown + 1, best, str + '.'), best);
best = Math.Min(Solve(blacks, lineNum + 1, shown + 1, best, str + 'x'), best);
best = Math.Min(Solve(allLCDs, lineNum + 1, shown, best, str + '?'), best);
return Solve(allLCDs, lineNum + 1, shown, best, str + '?');
private static void WriteLCD(string lcd)
private static void WriteDigit(string digit)
Console.WriteLine(string.Format(" {1}{1}{0}{2} {3}{0}{2} {3}{0} {4}{4}{0}{5} {6}{0}{5} {6}{0} {7}{7}{0}", Environment.NewLine, digit[0], digit[1], digit[2], digit[3], digit[4], digit[5], digit[6]));
private static string EquationToLCD(int[] equation)
var builder = new StringBuilder();
foreach (var digit in equation)
builder.Append(Digits[digit]);
return builder.ToString();