AdventOfCode2021/day4.cs
2022-12-17 12:31:21 +01:00

146 lines
3.4 KiB
C#

using System.Diagnostics.Metrics;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
class day4
{
string[] input;
string[] number = "7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1".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()
{
List<Field> fields = new List<Field>();
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;
}
foreach(Field field in fields)
{
field.markNumber("1");
field.printField();
field.checkBingo();
}
}
public void part2()
{
}
}
class Field{
string[,] bingoField = new string[5,5];
public Field(string[,] bingoField){
this.bingoField = bingoField;
}
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}]";
}
}
}
}
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;
}
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");
}
}