62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
using System.Globalization;
|
|
|
|
namespace part2;
|
|
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
// The description does not say this (had to go to reddit to find this)
|
|
// but the strings can combine the same letter for 2 unique numbers
|
|
// i.e. sevenine is "79" not "7ine"
|
|
|
|
int sum = 0;
|
|
|
|
string letters = "abcdefghijklmnopqrstuvwxyz";
|
|
Dictionary<string, int> written_numbers = new() {
|
|
{"one", 1},
|
|
{"two", 2},
|
|
{"three", 3},
|
|
{"four", 4},
|
|
{"five", 5},
|
|
{"six", 6},
|
|
{"seven", 7},
|
|
{"eight", 8},
|
|
{"nine", 9}
|
|
};
|
|
|
|
StreamReader sr = new StreamReader("input");
|
|
String line = sr.ReadLine();
|
|
//Continue to read until you reach end of file
|
|
while (line != null)
|
|
{
|
|
line = line.ToLower();
|
|
List<int> numbers_in_line = [];
|
|
|
|
for(int i = 0; i < line.Length; i++) {
|
|
foreach(var number in written_numbers) {
|
|
if(line[i] == number.Key[0]) {
|
|
if(string.Compare(line, i, number.Key, 0, number.Key.Length) == 0) {
|
|
numbers_in_line.Add(number.Value);
|
|
}
|
|
}
|
|
|
|
if(char.IsNumber(line[i])) {
|
|
numbers_in_line.Add(Int32.Parse(line[i].ToString()));
|
|
}
|
|
}
|
|
}
|
|
|
|
sum += numbers_in_line.First() * 10;
|
|
sum += numbers_in_line.Last();
|
|
|
|
Console.WriteLine($"Line: {line} - Sum: {sum}");
|
|
line = sr.ReadLine();
|
|
}
|
|
//close the file
|
|
sr.Close();
|
|
|
|
Console.WriteLine($"Total Sum: {sum}");
|
|
}
|
|
}
|