78 lines
2.0 KiB
C#
78 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AdvendOfCode2023
|
|
{
|
|
public class Day2
|
|
{
|
|
List<string> input = FileReader.Input("day2");
|
|
|
|
public int Day2PartOne()
|
|
{
|
|
var sum = 0;
|
|
|
|
foreach (var item in input)
|
|
{
|
|
var cubeSet = new CubeSet(item);
|
|
if (cubeSet.valid)
|
|
{
|
|
sum += cubeSet.id;
|
|
}
|
|
}
|
|
Console.WriteLine(sum);
|
|
return sum;
|
|
}
|
|
|
|
}
|
|
|
|
public class CubeSet
|
|
{
|
|
public int id = 0;
|
|
public int blue = 0;
|
|
public int green = 0;
|
|
public int red = 0;
|
|
public bool valid = true;
|
|
|
|
public CubeSet(string input)
|
|
{
|
|
input = input.Replace(",", "").Replace(";", "").Replace(":","");
|
|
|
|
var inputArray = input.Split(' ');
|
|
|
|
id = Convert.ToInt32(inputArray[1]);
|
|
|
|
for (var i = 1; i < inputArray.Length; i++)
|
|
{
|
|
if (inputArray[i] == "blue")
|
|
{
|
|
if(Convert.ToInt32(inputArray[i - 1]) > 14)
|
|
{
|
|
this.valid = false;
|
|
}
|
|
this.blue += Convert.ToInt32(inputArray[i - 1]);
|
|
}
|
|
if (inputArray[i] == "red")
|
|
{
|
|
if (Convert.ToInt32(inputArray[i - 1]) > 12)
|
|
{
|
|
this.valid = false;
|
|
}
|
|
this.red += Convert.ToInt32(inputArray[i - 1]);
|
|
}
|
|
if (inputArray[i] == "green")
|
|
{
|
|
if (Convert.ToInt32(inputArray[i - 1]) > 13)
|
|
{
|
|
this.valid = false;
|
|
}
|
|
this.green += Convert.ToInt32(inputArray[i - 1]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|