-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
34 lines (27 loc) · 966 Bytes
/
Program.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
using AdventOfCode.Common;
var lines = Resources.GetInputFileLines();
Console.WriteLine($"Part 1: {Part1(lines)}");
Console.WriteLine($"Part 2: {Part1(lines.Select(ReplaceDigits))}");
static int Part1(IEnumerable<string> lines) => lines
.Select(l => l.Where(char.IsDigit))
.Select(digits => 10 * digits.First() + digits.Last())
.Sum();
static string ReplaceDigits(string line)
{
string[] digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
for (int index = 0; index < line.Length; index++)
{
for (int d = 0; d < digits.Length; d++)
{
if (line[index..].StartsWith(digits[d]))
{
line =
line.Substring(0, index + 1)
+ d
+ (index < line.Length - 1 ? line.Substring(index + 1) : null);
index += digits[d].Length - 1;
}
}
}
return line;
}