feat(*): Add 2021-08 files (part 1 working, part 2 not)

This commit is contained in:
Josh Creek
2021-12-08 07:19:32 +00:00
parent 6b51590b8b
commit 12b0a55e53
5 changed files with 255 additions and 0 deletions
+118
View File
@@ -0,0 +1,118 @@
namespace Day08
{
class Program
{
static void Main(string[] args)
{
string[] lines = File.ReadAllLines("input.txt");
//Part1(lines);
Part2(lines);
}
static void Part1(string[] lines)
{
// 8 has only 7 segments/letters
// 7 has only 3 segments/letters
// 4 has only 4 segments/letters
// 1 has only 2 segments/letters
// Count how often groups of letters with 2/3/4/7 segments occur after the split
int count = 0;
foreach (string line in lines)
{
string secondHalf = line.Substring(line.IndexOf("|") + 1);
List<string> numberSegments = secondHalf.Split(' ').ToList();
foreach (string numberSegment in numberSegments)
{
switch (numberSegment.Length)
{
case 7:
case 3:
case 4:
case 2:
count += 1;
break;
default:
// code block
break;
}
}
}
Console.WriteLine(count);
}
static void Part2(string[] lines)
{
int total = 0;
foreach (string line in lines)
{
string secondHalf = line.Substring(line.IndexOf("|") + 1);
List<string> numberSegments = secondHalf.Split(' ').ToList();
string lineTotal = string.Empty;
foreach (string numberSegment in numberSegments)
{
// Go through sorted in alphabetical order
string alphabetisedNumberSegment = String.Concat(numberSegment.OrderBy(c => c));
if(alphabetisedNumberSegment == String.Concat("acedgfb".OrderBy(c => c)))
{
lineTotal += "8";
}
else if(alphabetisedNumberSegment == String.Concat("cdfbe".OrderBy(c => c)))
{
lineTotal += "5";
}
else if(alphabetisedNumberSegment == String.Concat("gcdfa".OrderBy(c => c)))
{
lineTotal += "2";
}
else if(alphabetisedNumberSegment == String.Concat("fbcad".OrderBy(c => c)))
{
lineTotal += "3";
}
else if(alphabetisedNumberSegment == String.Concat("dab".OrderBy(c => c)))
{
lineTotal += "7";
}
else if(alphabetisedNumberSegment == String.Concat("cefabd".OrderBy(c => c)))
{
lineTotal += "9";
}
else if(alphabetisedNumberSegment == String.Concat("cdfgeb".OrderBy(c => c)))
{
lineTotal += "6";
}
else if(alphabetisedNumberSegment == String.Concat("eafb".OrderBy(c => c)))
{
lineTotal += "4";
}
else if(alphabetisedNumberSegment == String.Concat("cagedb".OrderBy(c => c)))
{
lineTotal += "0";
}
else if(alphabetisedNumberSegment == String.Concat("ab".OrderBy(c => c)))
{
lineTotal += "1";
}
}
Console.WriteLine(lineTotal);
if (!string.IsNullOrEmpty(lineTotal))
{
total += int.Parse(lineTotal);
}
}
Console.WriteLine(total);
}
}
}