AoC 2023: Day 2
- Also updated to .NET 8.0 Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
a677039025
commit
8c20f2ce7e
|
@ -2,7 +2,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
namespace AdventOfCode.Year2023;
|
||||
|
||||
public static class Day2
|
||||
{
|
||||
private static List<string> Input = File.ReadAllLines("inputs/Year2023/day2.txt").ToList();
|
||||
|
||||
private class Game
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public List<Dictionary<string, int>> Sets { get; set; } = new List<Dictionary<string, int>>();
|
||||
}
|
||||
private static List<Game> Games = new List<Game>();
|
||||
|
||||
public static void Run()
|
||||
{
|
||||
ParseGames();
|
||||
Console.WriteLine($@"
|
||||
Day2 Solution
|
||||
Part1 Result: {Part1()}
|
||||
Part2 Result: {Part2()}
|
||||
|
||||
=============================");
|
||||
}
|
||||
|
||||
private static int Part1()
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
foreach (var game in Games)
|
||||
{
|
||||
foreach (var set in game.Sets)
|
||||
if (set["red"] > 12 || set["green"] > 13 || set["blue"] > 14) goto cont;
|
||||
sum += game.Id;
|
||||
cont: continue;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static int Part2()
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
foreach (var game in Games)
|
||||
{
|
||||
Dictionary<string, int> minimum = new Dictionary<string, int> {
|
||||
{"red", 0},
|
||||
{"green", 0},
|
||||
{"blue", 0}
|
||||
};
|
||||
foreach (var set in game.Sets)
|
||||
{
|
||||
foreach (var color in set.Keys)
|
||||
{
|
||||
if (minimum[color] < set[color]) minimum[color] = set[color];
|
||||
}
|
||||
}
|
||||
sum += minimum.Values.ToArray().Product();
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static void ParseGames()
|
||||
{
|
||||
foreach (var line in Input)
|
||||
{
|
||||
Game game = new Game();
|
||||
|
||||
string[] game_sets = line.Split(": ");
|
||||
game.Id = Convert.ToInt32(game_sets[0].Split(" ")[1]);
|
||||
|
||||
string[] sets = game_sets[1].Split("; ");
|
||||
|
||||
foreach (var setStr in sets)
|
||||
{
|
||||
Dictionary<string, int> set = new Dictionary<string, int>();
|
||||
foreach (var color in setStr.Split(", "))
|
||||
{
|
||||
set.Add(color.Split(" ")[1], Convert.ToInt32(color.Split(" ")[0]));
|
||||
}
|
||||
if (!set.ContainsKey("red")) set.Add("red", 0);
|
||||
if (!set.ContainsKey("green")) set.Add("green", 0);
|
||||
if (!set.ContainsKey("blue")) set.Add("blue", 0);
|
||||
game.Sets.Add(set);
|
||||
}
|
||||
|
||||
Games.Add(game);
|
||||
}
|
||||
}
|
||||
|
||||
private static int Product(this int[] array)
|
||||
{
|
||||
int result = 1;
|
||||
foreach (var e in array)
|
||||
result *= e;
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ public static class Loader2023
|
|||
*/
|
||||
|
||||
Day1.Run();
|
||||
Day2.Run();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue