| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using System.Text.RegularExpressions;
- namespace Day1;
- public partial class WordToDigitTransform : IStringTransform
- {
-
- [GeneratedRegex("one|two|three|four|five|six|seven|eight|nine", RegexOptions.IgnoreCase, "en-US")]
- private static partial Regex NumberWords();
-
- private static readonly Dictionary<string, char> Digits = new()
- {
- ["one"] = '1',
- ["two"] = '2',
- ["three"] = '3',
- ["four"] = '4',
- ["five"] = '5',
- ["six"] = '6',
- ["seven"] = '7',
- ["eight"] = '8',
- ["nine"] = '9',
- };
- public string Transform(string input)
- {
- var result = new List<char>();
- for (var i = 0; i < input.Length; i++)
- {
- var current = input.Substring(i);
- var next = false;
- foreach (var kvp in Digits)
- {
- if (current.StartsWith(kvp.Key))
- {
- result.Add(kvp.Value);
- next = true;
- break;
- }
- }
- if (!next)
- {
- result.Add(current[0]);
- }
- }
- return string.Concat(result);
- }
- }
|