re-added day 1 and 2

This commit is contained in:
Rob 2023-12-04 21:05:23 +01:00
parent e59dd074fd
commit 13340e6319
2 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,65 @@
using AdventOfCode.Core;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
public class Day01(InputReader reader) : IChallange
{
private readonly List<string> NumberMapping = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
private InputReader _inputReader = reader;
public async Task<string> GetSolutionPart1()
{
int sum = 0;
await foreach(string line in _inputReader.ReadAsStringLine())
{
// get all the numers
MatchCollection collection = Regex.Matches(line, @"\d");
// merge first and last index as one
string values = collection[0].Value + collection[^1].Value;
// make an int
sum += int.Parse(values);
}
return sum.ToString();
}
public async Task<string> GetSolutionPart2()
{
int sum = 0;
await foreach (string line in _inputReader.ReadAsStringLine())
{
string regex = @"(\d|one|two|three|four|five|six|seven|eight|nine)";
MatchCollection matchCollection = Regex.Matches(line, @"(\d)");
List<Match> captures = matchCollection.ToList();
// replace the text with numbers
captures.AddRange(Regex.Matches(line, @"(one)"));
captures.AddRange(Regex.Matches(line, @"(two)"));
captures.AddRange(Regex.Matches(line, @"(three)"));
captures.AddRange(Regex.Matches(line, @"(four)"));
captures.AddRange(Regex.Matches(line, @"(five)"));
captures.AddRange(Regex.Matches(line, @"(six)"));
captures.AddRange(Regex.Matches(line, @"(seven)"));
captures.AddRange(Regex.Matches(line, @"(eight)"));
captures.AddRange(Regex.Matches(line, @"(nine)"));
captures = captures.OrderBy(x => x.Index).ToList();
// merge first and last index as one
string values = GetStringAsNumber(captures.First().Value) + GetStringAsNumber(captures.Last().Value);
// make an int
sum += int.Parse(values);
}
return sum.ToString();
}
private string GetStringAsNumber(string value)
{
return value.Length == 1 ? value : NumberMapping.IndexOf(value).ToString();
}
}
}

View File

@ -0,0 +1,79 @@
using AdventOfCode.Core;
using System.Text.RegularExpressions;
namespace AdventOfCode.Solutions._2023
{
public class Day02(InputReader reader) : IChallange
{
private InputReader _inputReader = reader;
public async Task<string> GetSolutionPart1()
{
int sum = 0;
await foreach(string line in _inputReader.ReadAsStringLine())
{
// get all the numers
string gameId = line.Split(':')[0].Split(' ')[1];
string[] dicePulls = line.Split(':')[1].Split(';');
bool impossible = false;
// parse the pulls
foreach (string dicePull in dicePulls)
{
// regex the pull
MatchCollection dice = Regex.Matches(dicePull, "(\\d+) (g|r|b)");
foreach (Match match in dice)
{
string[] splitted = match.Value.Split(' ');
int ammount = int.Parse(splitted[0]);
if (ammount <= 12) // pass for all dice
continue;
if (ammount > 14 // fail for any dice set
|| (splitted[1] == "r" && ammount > 12)
|| (splitted[1] == "g" && ammount > 13))
impossible = true; // not possible
if (impossible) break;
}
if (impossible) break;
}
if (!impossible)
{
sum += int.Parse(gameId);
}
}
return sum.ToString();
}
public async Task<string> GetSolutionPart2()
{
int sum = 0;
await foreach (string line in _inputReader.ReadAsStringLine())
{
// get all the numers
string dicePulls = line.Split(':')[1];
// parse the pulls
MatchCollection dice = Regex.Matches(dicePulls, "(\\d+) (g|r|b)");
int red = 0, green = 0, blue = 0;
foreach (Match match in dice)
{
string[] splitted = match.Value.Split(' ');
int ammount = int.Parse(splitted[0]);
if (splitted[1] == "r" && ammount > red) { red = ammount; }
else if (splitted[1] == "g" && ammount > green) { green = ammount; }
else if (splitted[1] == "b" && ammount > blue) { blue = ammount; }
}
sum += red * green * blue;
}
return sum.ToString();
}
}
}