day 1 pt.2
This commit is contained in:
parent
04f571715f
commit
34fe74c9d5
@ -1,11 +1,56 @@
|
|||||||
using System.Buffers;
|
using System.Buffers;
|
||||||
using System.IO.Pipelines;
|
using System.IO.Pipelines;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace AdventOfCode;
|
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 class Result
|
||||||
{
|
{
|
||||||
public int Sum;
|
public int Sum;
|
||||||
@ -121,27 +166,67 @@ public static class Day1
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static int ParseString(string str)
|
public static int ParseString(string str)
|
||||||
{
|
{
|
||||||
var numOfDigits = 0;
|
var strSpan = str.AsSpan();
|
||||||
char? firstDigit = default;
|
int? firstDigit = default;
|
||||||
char? lastDigit = 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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
numOfDigits++;
|
foreach (var kv in matchers)
|
||||||
firstDigit ??= ch;
|
{
|
||||||
lastDigit = ch;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastDigit ??= firstDigit;
|
||||||
|
|
||||||
return !int.TryParse($"{firstDigit.Value}{lastDigit.Value}", out var result)
|
return !int.TryParse($"{firstDigit.Value}{lastDigit.Value}", out var result)
|
||||||
? 0
|
? 0
|
||||||
: result;
|
: result;
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
// See https://aka.ms/new-console-template for more information
|
// See https://aka.ms/new-console-template for more information
|
||||||
|
|
||||||
|
using System.Diagnostics;
|
||||||
using AdventOfCode;
|
using AdventOfCode;
|
||||||
|
|
||||||
|
var sw = Stopwatch.StartNew();
|
||||||
var day1Result = await Day1.ExecuteAsync("input.txt");
|
var day1Result = await Day1.ExecuteAsync("input.txt");
|
||||||
Console.WriteLine($"Advent of code: Day 1 result: {day1Result}");
|
Console.WriteLine($"Advent of code: Day 1 result: {day1Result}");
|
||||||
|
sw.Stop();
|
||||||
|
Console.WriteLine($"Advent of code: Day 1 time: {sw.Elapsed.TotalMilliseconds} ms.");
|
Loading…
Reference in New Issue
Block a user