-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day01Part2.cs
53 lines (46 loc) · 1.46 KB
/
Day01Part2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Text.RegularExpressions;
using AdventOfCode.Util;
using Microsoft.Extensions.Logging;
namespace AdventOfCode.Solutions.Day01;
[Solution("Day01", "Part2")]
public class Day01Part2 : Day01
{
public Day01Part2(ILogger<Day01Part2> logger) : base(logger) {}
protected override int GetLineValue(string line)
{
// Digits are processed separately to handle overlapping matches
var firstDigit = MapDigit
(
Expressions
.Digits(includingWords: true)
.Match(line)
.Value
);
var lastDigit = MapDigit
(
Expressions
// RTL ensures that we get the last match, even if it overlaps.
// .NET specific - the RightToLeft feature is not universal among regex engines.
.Digits(includingWords: true, rightToLeft: true)
.Match(line)
.Value
);
// Concat *then* parse
return int.Parse(firstDigit + lastDigit);
}
// Nothing fancy - let the compiler deal with this.
private static string MapDigit(string value)
=> value switch
{
"one" => "1",
"two" => "2",
"three" => "3",
"four" => "4",
"five" => "5",
"six" => "6",
"seven" => "7",
"eight" => "8",
"nine" => "9",
_ => value
};
}