Skip to content

Commit 60ccb52

Browse files
committed
D1: More concise and somehow faster...
1 parent 860c8c4 commit 60ccb52

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

Solutions/Day01/Day01.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
using System.Collections.Immutable;
21
using Formula9.AdventOfCode.Utils;
32

43
namespace Formula9.AdventOfCode.Solutions2023.Day01;
54

65
public class Day_01 : AdventOfCodeProblem
76
{
8-
public Day_01() : base(2023, 1) { }
7+
private string[] SplitInput { get; init; }
8+
9+
public Day_01() : base(2023, 1)
10+
{
11+
SplitInput = Input.SplitByNewline();
12+
}
913

1014
private static readonly IDictionary<string, string> Digits = new Dictionary<string, string>()
1115
{
@@ -18,17 +22,13 @@ public Day_01() : base(2023, 1) { }
1822
{ "seven", "s7n" },
1923
{ "eight", "e8t" },
2024
{ "nine", "n9e" }
21-
}.ToImmutableDictionary();
25+
};
2226

23-
private static int FindDigitPair(string s)
24-
{
25-
string altered = s.ReplaceAll(Digits);
26-
char first = altered.First(char.IsDigit);
27-
char last = altered.Last(char.IsDigit);
28-
return (first - '0') * 10 + (last - '0');
29-
}
27+
private static int FindDigitPair(string s) => SumFirstAndLastDigit(s.ReplaceAll(Digits));
28+
29+
private static int SumFirstAndLastDigit(string s) => (s.First(char.IsDigit) - '0') * 10 + (s.Last(char.IsDigit) - '0');
3030

31-
public override ValueTask<string> Solve_1() => new(Input.SplitByNewline().Select(l => int.Parse(new char[] { l.First(char.IsDigit), l.Last(char.IsDigit) })).Sum().ToString());
31+
public override ValueTask<string> Solve_1() => new(SplitInput.Select(SumFirstAndLastDigit).Sum().ToString());
3232

33-
public override ValueTask<string> Solve_2() => new(Input.SplitByNewline().Select(FindDigitPair).Sum().ToString());
33+
public override ValueTask<string> Solve_2() => new(SplitInput.Select(FindDigitPair).Sum().ToString());
3434
}

0 commit comments

Comments
 (0)