From a6cf619f0028450330dd95930c316bc8eaf8e325 Mon Sep 17 00:00:00 2001 From: Rob Stoffelen Date: Fri, 1 Dec 2023 09:27:36 +0100 Subject: [PATCH] welp, regex change did not work --- .../2023/Day 01/Day01Part2.cs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Advent Of Code Library/2023/Day 01/Day01Part2.cs b/Advent Of Code Library/2023/Day 01/Day01Part2.cs index dba08f5..0026738 100644 --- a/Advent Of Code Library/2023/Day 01/Day01Part2.cs +++ b/Advent Of Code Library/2023/Day 01/Day01Part2.cs @@ -19,17 +19,31 @@ private int GetValueFromLine(string line) { - // get all the items - List matches = Regex.Matches(line, @"(\d|one|two|three|four|five|six|seven|eight|nine)").OrderBy(x => x.Index).ToList(); + 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(matches[0].Value) + GetStringAsNumber(matches[^1].Value); + string values = GetStringAsNumber(captures.First().Value) + GetStringAsNumber(captures.Last().Value); // make an int return int.Parse(values); } private string GetStringAsNumber(string value) { - switch (value) + switch(value) { case "one": case "1":