using System.Diagnostics.Metrics; using System.Runtime.CompilerServices; using System.Security.Cryptography.X509Certificates; using System.Xml; class day4 { string[] input; string[] number = "37,60,87,13,34,72,45,49,61,27,97,88,50,30,76,40,63,9,38,67,82,6,59,90,99,54,11,66,98,23,64,14,18,4,10,89,46,32,19,5,1,53,25,96,2,12,86,58,41,68,95,8,7,3,85,70,35,55,77,44,36,51,15,52,56,57,91,16,71,92,84,17,33,29,47,75,80,39,83,74,73,65,78,69,21,42,31,93,22,62,24,48,81,0,26,43,20,28,94,79".Split(','); public day4(string day) { input = getInput(day); part1(); part2(); } public string[] getInput(string day) { string filepath = $"../../../inputs/{day}.txt"; string[] file = File.ReadAllLines(filepath); return file; } public void part1() { int result = 0; List fields = new List(); int index = 0 ; while (index <= input.Length) { int rowIndex = 0; var bingoField = new string[5, 5]; while (rowIndex < 5) { for(int i = 0; i < 5; i++) { bingoField[rowIndex,i] = $"[{input[index + rowIndex].Split(' ',StringSplitOptions.RemoveEmptyEntries)[i]}]"; } rowIndex++; } fields.Add(new Field(bingoField)); index += 6; } string markedNumber = ""; bool bingo = false; foreach (string mNumber in number) { while (bingo == false) { foreach (Field field in fields) { markedNumber = mNumber; field.markNumber(mNumber); if (field.checkBingo()) { Console.WriteLine(mNumber); field.printField(); result += field.checkUmarkedNumbers(); bingo= true; break; } } break; } } int markedNumberI = int.Parse(markedNumber); Console.WriteLine($"Part 1: {result * markedNumberI}"); } public void part2() { int result = 0; List fields = new List(); Console.WriteLine($"Part 2: {result}"); } } // Fiel Class wiht field Methods class Field { string[,] bingoField = new string[5, 5]; public Field(string[,] bingoField) { this.bingoField = bingoField; } // Will mark the actualy number in the cyclus with a X public void markNumber(string markNumber) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (bingoField[i, j].Contains($"[{markNumber}]")) { bingoField[i, j] = $"[X{markNumber}]"; } } } } // Checking row (horizontal) column (vertikal) if a Bingo is happens public bool checkBingo() { bool bingo = false; int counter = 0; int rowNr = 0; int columnNr = 0; while (rowNr < 5) { for (int i = 0; i < 5; i++) { if (bingoField[rowNr, i].Contains("X")) { counter++; if (counter == 5) { bingo = true; Console.WriteLine("Bingo"); return bingo; } } } counter = 0; rowNr++; } while (columnNr < 5) { for (int i = 0; i < 5; i++) { if (bingoField[i, columnNr].Contains("X")) { counter++; if (counter == 5) { bingo = true; Console.WriteLine("Bingo"); return bingo; } } } counter = 0; columnNr++; } return bingo; } // Simple field Print public void printField() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { Console.Write(bingoField[i, j]); } Console.WriteLine(" "); } Console.WriteLine("\n"); } // Returns a sum of all unmarked numbers in the field public int checkUmarkedNumbers() { int result = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (!bingoField[i, j].Contains("X")) { string tmp = bingoField[i, j].Substring(1, bingoField[i, j].Length - 2); result += int.Parse(tmp); } } } return result; } }