using System.Collections.Generic;
var positionsPerDigit = new Dictionary<int, char[]>
[0] = new[] { 'a', 'b', 'c', 'e', 'f', 'g' },
[1] = new[] { 'c', 'f' },
[2] = new[] { 'a', 'c', 'd', 'e', 'g' },
[3] = new[] { 'a', 'c', 'd', 'f', 'g' },
[4] = new[] { 'b', 'c', 'd', 'f' },
[5] = new[] { 'a', 'b', 'd', 'f', 'g' },
[6] = new[] { 'a', 'b', 'd', 'e', 'f', 'g' },
[7] = new[] { 'a', 'c', 'f' },
[8] = new[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g' },
[9] = new[] { 'a', 'b', 'c', 'd', 'f', 'g' },
var digitsWithUniqueNumberOfPositions = new[] { 1, 4, 7, 8 };
var response = await new HttpClient().GetStringAsync("https://tompafireadventofcode.free.beeceptor.com/day8");
var lines = response.Split("\n");
var inputOutput = lines.Select(line =>
return line.Split(" | ");
var inputLines = inputOutput.Select(line => line.First().Split(' '));
var outputLines = inputOutput.Select(line => line.Last().Split(' '));
var outputs = outputLines.SelectMany(x => x);
var outputsPerNumberOfPositions = outputs.GroupBy(o => o.Length).Select(o => new { NumberOfPoistions = o.Key, Count = o.Count() });
var digitsToLookFor = digitsWithUniqueNumberOfPositions;
var numberOfPositionsToLookFor = digitsToLookFor.Select(d => positionsPerDigit[d].Length).ToList();
var result = outputsPerNumberOfPositions.Where(o => numberOfPositionsToLookFor.Contains(o.NumberOfPoistions)).Sum(o => o.Count);
Console.WriteLine(result);
var result2 = inputLines.Select((inputs, index) =>
var digits = GetParsedOutputDigits(inputs, outputLines.ElementAt(index), positionsPerDigit);
return GetCombinedDigitsValue(digits);
Console.WriteLine(result2);
int GetCombinedDigitsValue(IEnumerable<int> digits)
var combined = int.Parse(string.Join("", digits.Select(d => d.ToString())));
IEnumerable<int> GetParsedOutputDigits(IEnumerable<string> inputs, IEnumerable<string> outputs, Dictionary<int, char[]> positionsPerDigit)
var digitPerInput = inputs.Select(input => Normalize(input)).Distinct()
.ToDictionary(input => input, input => input switch
{ } when input.Length == positionsPerDigit[1].Length => (int?)1,
{ } when input.Length == positionsPerDigit[4].Length => 4,
{ } when input.Length == positionsPerDigit[7].Length => 7,
{ } when input.Length == positionsPerDigit[8].Length => 8,
var unResolved = digitPerInput.Where(x => x.Value is null).Select(x => x.Key).ToList();
unResolved.ForEach(input => {
digitPerInput[input] = input switch
{ } when input.Length == 6 && GetMatchingPositions(input, digitPerInput.Single(d => d.Value == 1).Key).Count() == 1
{ } when input.Length == 6 && GetMatchingPositions(input, digitPerInput.Single(d => d.Value == 4).Key).Count() == 4
{ } when input.Length == 6
{ } when input.Length == 5 && GetMatchingPositions(input, digitPerInput.Single(d => d.Value == 1).Key).Count() == 2
{ } when input.Length == 5 && GetMatchingPositions(input, digitPerInput.Single(d => d.Value == 4).Key).Count() == 3
return outputs.Select(output =>
return digitPerInput[Normalize(output)].GetValueOrDefault();
IEnumerable<char> GetMatchingPositions(IEnumerable<char> positions, IEnumerable<char> positions2)
return positions.Intersect(positions2);
string Normalize(string positions)
return new string(positions.OrderBy(x => x).ToArray());