diff --git a/AdventOfCode.Solutions/2023/Day 01/Day01.cs b/AdventOfCode.Solutions/2023/Day 01/Day01.cs new file mode 100644 index 0000000..bf4a73c --- /dev/null +++ b/AdventOfCode.Solutions/2023/Day 01/Day01.cs @@ -0,0 +1,65 @@ +using AdventOfCode.Core; +using System.Text.RegularExpressions; + +namespace AdventOfCode.Solutions._2023 +{ + public class Day01(InputReader reader) : IChallange + { + private readonly List NumberMapping = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; + + private InputReader _inputReader = reader; + + public async Task 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 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 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(); + } + } +} \ No newline at end of file diff --git a/AdventOfCode.Solutions/2023/Day 02/Day02.cs b/AdventOfCode.Solutions/2023/Day 02/Day02.cs new file mode 100644 index 0000000..c3584ed --- /dev/null +++ b/AdventOfCode.Solutions/2023/Day 02/Day02.cs @@ -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 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 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(); + } + } +} \ No newline at end of file