Initial Commit
This commit is contained in:
147
2023/Day02CSharp/Program.cs
Normal file
147
2023/Day02CSharp/Program.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
using AdventCommon;
|
||||
|
||||
var p1exampleSln = Part01("example-input.txt");
|
||||
var p1puzzleSln = Part01("puzzle-input.txt");
|
||||
|
||||
Console.WriteLine("Part01 Example Sum: " + p1exampleSln);
|
||||
Console.WriteLine("Part01 Puzzle Sum: " + p1puzzleSln);
|
||||
|
||||
var p2exampleSln = Part02("example-input.txt");
|
||||
var p2puzzleSln = Part02("puzzle-input.txt");
|
||||
|
||||
Console.WriteLine("Part02 Example Sum: " + p2exampleSln);
|
||||
Console.WriteLine("Part02 Puzzle Sum: " + p2puzzleSln);
|
||||
|
||||
int Part01(string fileName)
|
||||
{
|
||||
var input = new PuzzleInput(fileName);
|
||||
|
||||
var trueBag = new Dictionary<string, int>() { { "red", 12 },
|
||||
{ "green", 13 },
|
||||
{ "blue", 14 }
|
||||
};
|
||||
var bag = new BagOfCubes(trueBag);
|
||||
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < input.Lines.Count; i++)
|
||||
{
|
||||
var game = BagOfCubes.ParseLine(input.Lines[i]);
|
||||
|
||||
bool possible = true;
|
||||
|
||||
foreach (var item in game)
|
||||
{
|
||||
if (bag.IsPossible(item.Key, item.Value))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
possible = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (possible)
|
||||
{
|
||||
sum += i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
int Part02(string fileName)
|
||||
{
|
||||
int sum = 0;
|
||||
var input = new PuzzleInput(fileName);
|
||||
|
||||
foreach (var item in input.Lines)
|
||||
{
|
||||
var result = BagOfCubes.ParseLine(item);
|
||||
|
||||
int power = 1;
|
||||
foreach (var item2 in result)
|
||||
{
|
||||
power *= item2.Value;
|
||||
}
|
||||
|
||||
sum += power;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
class BagOfCubes
|
||||
{
|
||||
public BagOfCubes(PuzzleInput input)
|
||||
{
|
||||
foreach (var line in input.Lines)
|
||||
{
|
||||
var result = ParseLine(line);
|
||||
|
||||
foreach (var item in result)
|
||||
{
|
||||
if (!Cubes.ContainsKey(item.Key))
|
||||
{
|
||||
Cubes.Add(item.Key, item.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
Cubes[item.Key] = Math.Max(item.Value, Cubes[item.Key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BagOfCubes(Dictionary<string, int> cubes)
|
||||
{
|
||||
Cubes = cubes;
|
||||
}
|
||||
|
||||
public static Dictionary<string, int> ParseLine(string line)
|
||||
{
|
||||
Dictionary<string, int> toRet = new Dictionary<string, int>();
|
||||
|
||||
var plays = line.Split(':')[1].Split(';', StringSplitOptions.TrimEntries);
|
||||
|
||||
foreach (var play in plays)
|
||||
{
|
||||
var p = play.Split(',', StringSplitOptions.TrimEntries);
|
||||
|
||||
foreach (var split in p)
|
||||
{
|
||||
var cubeCount = Int32.Parse(split.Split(' ')[0]);
|
||||
var cubeColor = split.Split(' ', StringSplitOptions.TrimEntries)[1];
|
||||
|
||||
if (!toRet.ContainsKey(cubeColor))
|
||||
{
|
||||
toRet.Add(cubeColor, cubeCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
toRet[cubeColor] = Math.Max(cubeCount, toRet[cubeColor]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return toRet;
|
||||
}
|
||||
|
||||
public int GetCubeCount(string color)
|
||||
{
|
||||
if (Cubes.ContainsKey(color))
|
||||
{
|
||||
return Cubes[color];
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public bool IsPossible(string color, int count)
|
||||
{
|
||||
return GetCubeCount(color) >= count;
|
||||
}
|
||||
|
||||
public Dictionary<string, int> Cubes { get; private set; } = new Dictionary<string, int>();
|
||||
}
|
||||
Reference in New Issue
Block a user