Skip to content

Commit 219293d

Browse files
committed
2023/01 with regex
1 parent c52144e commit 219293d

File tree

1 file changed

+23
-38
lines changed

1 file changed

+23
-38
lines changed

2023/Day01/Solution.cs

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,37 @@
1-
using System;
21
using System.Linq;
2+
using System.Text.RegularExpressions;
33

44
namespace AdventOfCode.Y2023.Day01;
55

66
[ProblemName("Trebuchet?!")]
77
class Solution : Solver {
88

99
public object PartOne(string input) =>
10-
Solve(input, Digit);
10+
Solve(input, @"\d");
1111

1212
public object PartTwo(string input) =>
13-
Solve(input, st => Digit(st) ?? SpelledOutNumber(st));
13+
Solve(input, @"\d|one|two|three|four|five|six|seven|eight|nine");
1414

15-
int? Digit(string st) => char.IsDigit(st[0]) ? st[0] - '0' : null;
15+
int Solve(string input, string rx) =>
16+
input.Split("\n").Select(line => GetNumber(line, rx)).Sum();
17+
18+
int GetNumber(string line, string rx) {
19+
var first = Regex.Match(line, rx);
20+
var last = Regex.Match(line, rx, RegexOptions.RightToLeft);
1621

17-
int? SpelledOutNumber(string st) =>
18-
st.StartsWith("one") ? 1 :
19-
st.StartsWith("two") ? 2 :
20-
st.StartsWith("three") ? 3 :
21-
st.StartsWith("four") ? 4 :
22-
st.StartsWith("five") ? 5 :
23-
st.StartsWith("six") ? 6 :
24-
st.StartsWith("seven") ? 7 :
25-
st.StartsWith("eight") ? 8 :
26-
st.StartsWith("nine") ? 9 :
27-
null;
28-
29-
int Solve(string input, Func<string, int?> parser) =>
30-
input.Split("\n").Select(line => GetNumber(line, parser)).Sum();
31-
32-
// Go over the line from both ends using the parser and find the first numbers
33-
// in both directions, these will be the "first" and "last" digits.
34-
// Returns 0 if no numbers found.
35-
int GetNumber(string line, Func<string, int?> parse) {
36-
int? first = null;
37-
int? last = null;
38-
39-
for (var i =0; i < line.Length; i++) {
40-
var prefix = line[i..];
41-
first = first ?? parse(prefix);
42-
43-
var suffix = line[(line.Length - i - 1)..];
44-
last = last ?? parse(suffix);
45-
46-
if (first.HasValue && last.HasValue) {
47-
return first.Value * 10 + last.Value;
48-
}
49-
}
50-
return 0;
22+
return ParseMatch(first) * 10 + ParseMatch(last);
5123
}
24+
25+
int ParseMatch(Match m) =>
26+
!m.Success ? 0 :
27+
m.Groups[0].Value == "one" ? 1 :
28+
m.Groups[0].Value == "two" ? 2 :
29+
m.Groups[0].Value == "three" ? 3 :
30+
m.Groups[0].Value == "four" ? 4 :
31+
m.Groups[0].Value == "five" ? 5 :
32+
m.Groups[0].Value == "six" ? 6 :
33+
m.Groups[0].Value == "seven" ? 7 :
34+
m.Groups[0].Value == "eight" ? 8 :
35+
m.Groups[0].Value == "nine" ? 9 :
36+
int.Parse(m.Groups[0].Value);
5237
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy