-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_1.cs
105 lines (84 loc) · 2.95 KB
/
Day_1.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.Text.RegularExpressions;
namespace AdventOfCode_2023
{
public class Day_1 : Day
{
public override long FirstPart()
{
long sum = 0;
foreach (string input in inputs)
{
string firstDigit = null;
string lastDigit = null;
string patternDouble = @"\D*(\d).*(\d).*";
string patternSingle = @"\D*(\d).*";
Match match = Regex.Match(input, patternDouble);
if (match.Success)
{
firstDigit = match.Groups[1].Value;
lastDigit = match.Groups[2].Value;
}
else
{
match = Regex.Match(input, patternSingle);
firstDigit = match.Groups[1].Value;
lastDigit = firstDigit;
}
string inputNumber = $"{firstDigit}{lastDigit}";
sum += long.Parse(inputNumber);
}
return sum;
}
public override long SecondPart()
{
long sum = 0;
IDictionary<string, string> numbersString = new Dictionary<string, string>
{
{"oneight", "18"},
{"twone", "21"},
{"threeight", "38"},
{"fiveight", "58"},
{"sevenine", "79"},
{"eightwo", "82"},
{"eighthree", "83"},
{"nineight", "98"},
{"one", "1"},
{"two", "2"},
{"three", "3"},
{"four", "4"},
{"five", "5"},
{"six", "6"},
{"seven", "7"},
{"eight", "8"},
{"nine" ,"9"},
};
foreach (string input in inputs)
{
string sanitizedInput = input;
foreach(var kvp in numbersString)
{
sanitizedInput = sanitizedInput.Replace(kvp.Key, kvp.Value);
}
string firstDigit = null;
string lastDigit = null;
string patternDouble = @"\D*(\d).*(\d).*";
string patternSingle = @"\D*(\d).*";
Match match = Regex.Match(sanitizedInput, patternDouble);
if (match.Success)
{
firstDigit = match.Groups[1].Value;
lastDigit = match.Groups[2].Value;
}
else
{
match = Regex.Match(sanitizedInput, patternSingle);
firstDigit = match.Groups[1].Value;
lastDigit = firstDigit;
}
string inputNumber = $"{firstDigit}{lastDigit}";
sum += long.Parse(inputNumber);
}
return sum;
}
}
}