day 1 pt.2

This commit is contained in:
Kirill Vorotov 2023-12-08 20:25:56 +00:00
parent 04f571715f
commit 34fe74c9d5
2 changed files with 100 additions and 11 deletions

View File

@ -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<Regex, int> 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,27 +166,67 @@ public static class Day1
/// <returns></returns>
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;
}
if (firstDigit == null || lastDigit == null || numOfDigits < 1)
firstDigit ??= kv.Value;
break;
}
if (firstDigit != null)
{
break;
}
}
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
: result;

View File

@ -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}");
sw.Stop();
Console.WriteLine($"Advent of code: Day 1 time: {sw.Elapsed.TotalMilliseconds} ms.");