using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
namespace _2021___Day_08
{
class Program
{
static void Main(string[] args)
{
string[] input = File.ReadAllLines(@"../../../../Input.in");
Console.WriteLine(input.Select(x => x.Split("|")[1].Split(" ").Count(x => x.Length == 2 || x.Length == 3 || x.Length == 4 || x.Length == 7)).Sum().ToString() + " matching first criteria");
var result = 0;
foreach (var line in input)
{
var inputPattern = line.Split("|")[0].Split(" ").Where(x => x.Length == 2 || x.Length == 3 || x.Length == 4 || x.Length == 7).OrderBy(i => i.Length).ToArray();
var output = line.Split("|")[1].Trim().Split(" ");
var sb = new StringBuilder();
foreach (var disp in output)
{
sb.Append(disp.Length == 2 ? "1" : disp.Length == 3 ? "7" : disp.Length == 4 ? "4" : disp.Length == 7 ? "8" :
FindVal(inputPattern, disp));
}
result += int.Parse(sb.ToString());
}
Console.WriteLine("Sum of all is " + result.ToString());
}
public static string FindVal(string[] input, string line)
{
if (line.Length == 5)
{
if (input[0].All(x => line.Contains(x)))
{
return "3";
}
else if (input[3].Where(x => !input[2].Contains(x)).All(x => line.Contains(x)))
{
return "2";
}
else
{
return "5";
}
}
else if (line.Length == 6)
{
if (input[1].All(x => line.Contains(x)) && !input[2].All(x => line.Contains(x)))
{
return "0";
}
else if (input[2].All(x => line.Contains(x)))
{
return "9";
}
else
{
return "6";
}
}
return null;
}
}
}