Uploading Days 1-3
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
namespace day2;
|
||||
|
||||
public class Day2
|
||||
{
|
||||
private static IList<IDictionary<string, int>> _splitLineToCubeSets(string line)
|
||||
{
|
||||
var cubeSets = new List<IDictionary<string, int>>();
|
||||
var cubeSetStrings = line.Split(";");
|
||||
foreach (var cubeSetString in cubeSetStrings)
|
||||
{
|
||||
var cubeStrings = cubeSetString.Split(",");
|
||||
var newCubeSet = new Dictionary<string, int>();
|
||||
foreach (var cubeString in cubeStrings)
|
||||
{
|
||||
var splitCube = cubeString.Trim().Split(" ");
|
||||
newCubeSet.Add(splitCube[1], int.Parse(splitCube[0]));
|
||||
}
|
||||
cubeSets.Add(newCubeSet);
|
||||
}
|
||||
return cubeSets;
|
||||
}
|
||||
|
||||
private static bool _isGamePossible(string gameLine, IDictionary<string, int> expectedBag)
|
||||
{
|
||||
var parsedGame = _splitLineToCubeSets(gameLine);
|
||||
foreach (var parsedCubeSet in parsedGame)
|
||||
{
|
||||
var isSetPossible = parsedCubeSet.Aggregate(true, (current, kvp) => current && kvp.Value <= expectedBag[kvp.Key]);
|
||||
if (!isSetPossible)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static IDictionary<string, int> _minimumPossibleCubes(string gameLine)
|
||||
{
|
||||
var parsedGame = _splitLineToCubeSets(gameLine);
|
||||
var minimumCubes = new Dictionary<string, int>();
|
||||
foreach (var cubeSet in parsedGame)
|
||||
{
|
||||
foreach (var (color, frequency) in cubeSet)
|
||||
{
|
||||
minimumCubes[color] = minimumCubes.ContainsKey(color) ? Math.Max(minimumCubes[color], frequency) : frequency;
|
||||
}
|
||||
}
|
||||
|
||||
return minimumCubes;
|
||||
}
|
||||
|
||||
|
||||
public static int PartOne(string[] lines)
|
||||
{
|
||||
var finalResult = 0;
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var splitLine = line.Split(":").Select(s => s.Trim()).ToList();
|
||||
var gameNumber = int.Parse(splitLine[0].Split(" ")[1]);
|
||||
var gameLine = splitLine[1];
|
||||
var expectedBag = new Dictionary<string, int>() {
|
||||
{ "red", 12 },
|
||||
{ "green", 13 },
|
||||
{ "blue", 14 }
|
||||
};
|
||||
|
||||
if (_isGamePossible(gameLine, expectedBag))
|
||||
{
|
||||
finalResult += gameNumber;
|
||||
}
|
||||
}
|
||||
return finalResult;
|
||||
}
|
||||
|
||||
public static int PartTwo(string[] lines)
|
||||
{
|
||||
var finalResult = 0;
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var splitLine = line.Split(":").Select(s => s.Trim()).ToList();
|
||||
var gameNumber = int.Parse(splitLine[0].Split(" ")[1]);
|
||||
var gameLine = splitLine[1];
|
||||
|
||||
var minimumPossibleCubes = _minimumPossibleCubes(gameLine);
|
||||
var minimumRed = minimumPossibleCubes.ContainsKey("red") ? minimumPossibleCubes["red"] : 0;
|
||||
var minimumGreen = minimumPossibleCubes.ContainsKey("green") ? minimumPossibleCubes["green"] : 0;
|
||||
var minimumBlue = minimumPossibleCubes.ContainsKey("blue") ? minimumPossibleCubes["blue"] : 0;
|
||||
var power = minimumRed * minimumGreen * minimumBlue;
|
||||
|
||||
finalResult += power;
|
||||
}
|
||||
return finalResult;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
using day2;
|
||||
|
||||
var fileContents = File.ReadAllText(Path.GetFullPath("input.txt"));
|
||||
var lines = fileContents.Split(Environment.NewLine);
|
||||
Console.WriteLine($"The answer to Part 1 is {Day2.PartOne(lines)}");
|
||||
Console.WriteLine($"The answer to Part 2 is {Day2.PartTwo(lines)}");
|
||||
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user