From 34fe74c9d531b84c45a8d84228f322b37a382cfe Mon Sep 17 00:00:00 2001 From: Kirill Vorotov Date: Fri, 8 Dec 2023 20:25:56 +0000 Subject: [PATCH] day 1 pt.2 --- src/AdventOfCode/AdventOfCode/Day1.cs | 105 ++++++++++++++++++++++--- src/AdventOfCode/ConsoleApp/Program.cs | 6 +- 2 files changed, 100 insertions(+), 11 deletions(-) diff --git a/src/AdventOfCode/AdventOfCode/Day1.cs b/src/AdventOfCode/AdventOfCode/Day1.cs index 79fc83f..7ab56f1 100644 --- a/src/AdventOfCode/AdventOfCode/Day1.cs +++ b/src/AdventOfCode/AdventOfCode/Day1.cs @@ -1,11 +1,56 @@ using System.Buffers; using System.IO.Pipelines; using System.Text; +using System.Text.RegularExpressions; namespace AdventOfCode; -public static class Day1 +public static partial class Day1 { + [GeneratedRegex(pattern: "0", RegexOptions.Compiled | RegexOptions.IgnoreCase)] + private static partial Regex Zero(); + + [GeneratedRegex(pattern: "one|1", RegexOptions.Compiled | RegexOptions.IgnoreCase)] + private static partial Regex One(); + + [GeneratedRegex(pattern: "two|2", RegexOptions.Compiled | RegexOptions.IgnoreCase)] + private static partial Regex Two(); + + [GeneratedRegex(pattern: "three|3", RegexOptions.Compiled | RegexOptions.IgnoreCase)] + private static partial Regex Three(); + + [GeneratedRegex(pattern: "four|4", RegexOptions.Compiled | RegexOptions.IgnoreCase)] + private static partial Regex Four(); + + [GeneratedRegex(pattern: "five|5", RegexOptions.Compiled | RegexOptions.IgnoreCase)] + private static partial Regex Five(); + + [GeneratedRegex(pattern: "six|6", RegexOptions.Compiled | RegexOptions.IgnoreCase)] + private static partial Regex Six(); + + [GeneratedRegex(pattern: "seven|7", RegexOptions.Compiled | RegexOptions.IgnoreCase)] + private static partial Regex Seven(); + + [GeneratedRegex(pattern: "eight|8", RegexOptions.Compiled | RegexOptions.IgnoreCase)] + private static partial Regex Eight(); + + [GeneratedRegex(pattern: "nine|9", RegexOptions.Compiled | RegexOptions.IgnoreCase)] + private static partial Regex Nine(); + + private static readonly Dictionary matchers = new() + { + { Zero(), 0 }, + { One(), 1 }, + { Two(), 2 }, + { Three(), 3 }, + { Four(), 4 }, + { Five(), 5 }, + { Six(), 6 }, + { Seven(), 7 }, + { Eight(), 8 }, + { Nine(), 9 }, + }; + public class Result { public int Sum; @@ -121,26 +166,66 @@ public static class Day1 /// public static int ParseString(string str) { - var numOfDigits = 0; - char? firstDigit = default; - char? lastDigit = default; + var strSpan = str.AsSpan(); + int? firstDigit = default; + int? lastDigit = default; - foreach (var ch in str) + for (var index = 0; index < strSpan.Length; index++) { - if (!int.TryParse($"{ch}", out _)) + var span = strSpan[0 .. (index + 1)]; + if (span.IsEmpty) { continue; } - numOfDigits++; - firstDigit ??= ch; - lastDigit = ch; + foreach (var kv in matchers) + { + if (!kv.Key.IsMatch(span)) + { + continue; + } + + firstDigit ??= kv.Value; + break; + } + + if (firstDigit != null) + { + break; + } } - if (firstDigit == null || lastDigit == null || numOfDigits < 1) + for (var i = strSpan.Length - 1; i >= 0; i--) + { + var span = strSpan[i .. strSpan.Length]; + if (span.IsEmpty) + { + continue; + } + + foreach (var kv in matchers) + { + if (!kv.Key.IsMatch(span)) + { + continue; + } + + lastDigit ??= kv.Value; + break; + } + + if (lastDigit != null) + { + break; + } + } + + if (firstDigit == null) { return 0; } + + lastDigit ??= firstDigit; return !int.TryParse($"{firstDigit.Value}{lastDigit.Value}", out var result) ? 0 diff --git a/src/AdventOfCode/ConsoleApp/Program.cs b/src/AdventOfCode/ConsoleApp/Program.cs index 24bc39e..cc8e42e 100644 --- a/src/AdventOfCode/ConsoleApp/Program.cs +++ b/src/AdventOfCode/ConsoleApp/Program.cs @@ -1,6 +1,10 @@ // See https://aka.ms/new-console-template for more information +using System.Diagnostics; using AdventOfCode; +var sw = Stopwatch.StartNew(); var day1Result = await Day1.ExecuteAsync("input.txt"); -Console.WriteLine($"Advent of code: Day 1 result: {day1Result}"); \ No newline at end of file +Console.WriteLine($"Advent of code: Day 1 result: {day1Result}"); +sw.Stop(); +Console.WriteLine($"Advent of code: Day 1 time: {sw.Elapsed.TotalMilliseconds} ms."); \ No newline at end of file