AdventOfCode2021/day4.cs

184 lines
5.1 KiB
C#
Raw Normal View History

2022-12-17 11:57:30 +01:00
using System.Diagnostics.Metrics;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
class day4
{
string[] input;
2022-12-18 12:17:27 +01:00
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(',');
2022-12-17 11:57:30 +01:00
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()
{
2022-12-18 12:17:27 +01:00
int result = 0;
2022-12-17 11:57:30 +01:00
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;
}
2022-12-18 12:17:27 +01:00
string markedNumber = "";
bool bingo = false;
2022-12-17 11:57:30 +01:00
2022-12-18 12:17:27 +01:00
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}");
2022-12-17 11:57:30 +01:00
}
public void part2()
{
}
}
2022-12-18 12:17:27 +01:00
// Fiel Class wiht field Methods
2022-12-17 09:22:51 +01:00
class Field{
string[,] bingoField = new string[5,5];
public Field(string[,] bingoField){
this.bingoField = bingoField;
}
2022-12-18 12:17:27 +01:00
// Will mark the actualy number in the cyclus with a X
2022-12-17 09:22:51 +01:00
public void markNumber(string markNumber){
2022-12-17 11:57:30 +01:00
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
2022-12-17 19:18:51 +01:00
if (bingoField[i, j].Contains($"[{markNumber}]"))
2022-12-17 11:57:30 +01:00
{
2022-12-17 12:31:21 +01:00
bingoField[i, j] = $"[X{markNumber}]";
2022-12-17 11:57:30 +01:00
}
}
}
}
2022-12-18 12:17:27 +01:00
// Checking row (horizontal) column (vertikal) if a Bingo is happens
2022-12-17 12:31:21 +01:00
public bool checkBingo()
2022-12-17 11:57:30 +01:00
{
bool bingo = false;
2022-12-17 12:31:21 +01:00
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");
2022-12-18 12:17:27 +01:00
2022-12-17 12:31:21 +01:00
return bingo;
}
}
}
counter = 0;
columnNr++;
}
return bingo;
2022-12-17 11:57:30 +01:00
}
2022-12-18 12:17:27 +01:00
// Simple field Print
2022-12-17 11:57:30 +01:00
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");
2022-12-18 12:17:27 +01:00
}
2022-12-17 11:57:30 +01:00
2022-12-18 12:17:27 +01:00
// 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;
2022-12-17 09:22:51 +01:00
}
2022-12-18 12:17:27 +01:00
}