AdventOfCode2021/day3.cs

86 lines
2.1 KiB
C#
Raw Normal View History

2022-12-13 17:08:08 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdventOfCode2021
{
internal class day3
{
string[] input;
public day3(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()
{
var result = new StringBuilder();
var result2 = new StringBuilder();
for(int i = 0; i < input[0].Length; i++)
{
int oneCounter = 0;
int zeroCounter = 0;
for(int j = 0; j < input.Length; j++)
{
if (input[j][i] == '0')
{
zeroCounter++;
}
else
{
oneCounter++;
}
}
if (oneCounter > zeroCounter)
{
result.Append('1');
result2.Append('0');
}
else if (oneCounter < zeroCounter)
{
result.Append('0');
result2.Append('1');
}
else if(oneCounter == zeroCounter)
{
result.Append('1');
result2.Append('0');
}
}
int decResult1 = Convert.ToInt32(result.ToString(), 2);
int decResult2 = Convert.ToInt32(result2.ToString(), 2);
int finalResult = decResult1 * decResult2;
Console.WriteLine($"Part 1 : {finalResult}");
}
public void part2()
{
Console.WriteLine($"Part 2 : ");
}
}
}