-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.cs
53 lines (50 loc) · 1.91 KB
/
Day1.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
//Part 1
string[] input = File.ReadAllLines("input.txt");
long total = (from s in input
let first = s.First(char.IsDigit).ToString()
let last = s.Last(char.IsDigit).ToString()
select long.Parse(first + last)).Sum();
//Part 2
string[] input = File.ReadAllLines("input.txt");
Dictionary<string, string> numberDictionary = new Dictionary<string, string>
{
{"one","1"},
{"two","2"},
{"three","3"},
{"four","4"},
{"five","5"},
{"six","6"},
{"seven","7"},
{"eight","8"},
{"nine","9"}
};
long total = 0;
foreach (string s in input)
{
string first = s.First(char.IsDigit).ToString();
int firstIndex = s.IndexOf(first);
string last = s.Last(char.IsDigit).ToString();
int lastIndex = s.LastIndexOf(last);
foreach (KeyValuePair<string, string> kvp in numberDictionary)
{
int firstNumIndex = s.IndexOf(kvp.Key);
if (firstNumIndex > -1)
{
if (firstNumIndex < firstIndex)
{
firstIndex = firstNumIndex;
first = kvp.Value;
}
}
int lastNumIndex = s.LastIndexOf(kvp.Key);
if (lastNumIndex > -1)
{
if (lastNumIndex > lastIndex)
{
lastIndex = lastNumIndex;
last = kvp.Value;
}
}
}
total += long.Parse(first + last);
}