
Day 1 - Trebuchet
Simply put, this challenge requires the developer to retrieve numbers from lines of text, and then add up all the numbers from each line to get a total.
Part 1 was fairly straightforward. Select all the digits from within the text. This can easily be done via the LINQ Select method, and passing it a method which checks whether the current character is a digit and, if so returns it.
Once we have this, it's just a case of selecting the first and last items in the array, as we always know there will be more than 1 digit based on the challenge instructions.
Part 2 however had me pulling my hair out for a little while, as this was sneakily more complicated. I began by simply pulling out the text and numeric values using a lookup. However, this did not work due to there being overlap between numbers (text version), e.g. 'eightwo' etc.
To resolve simply replace the matched numbers (in text) format with their digit counterpart, prefixed and suffixed with their text format. This removes any cross over / overlap of words. It's a bit of a hacky solution, but it is one that worked for me (and others it seems online too).
Solution: Available also at my Githublink to Day 1
usingSystem.Runtime.ExceptionServices;usingSystem.Text;usingSystem.Text.RegularExpressions;stringFILE_PATH="./final_input.txt";Dictionary<string,string>translations=new(){{"one","one1one"},{"two","two2two"},{"three","three3three"},{"four","four4four"},{"five","five5five"},{"six","six6six"},{"seven","seven7seven"},{"eight","eight8eight"},{"nine","nine9nine"}};stringOnlyNumerics(stringstrInput){StringBuilderbuilder=new();foreach(charchinstrInput){if(char.IsDigit(ch))builder.Append(ch);}returnbuilder.ToString();}stringReplaceNumbers(stringoriginal){foreach(varkeyintranslations.Keys){original=original.Replace(key,translations[key].ToString());}returnoriginal;}intGetFirstAndLast(stringstr){intfirst=int.Parse(str[0].ToString());intlast=int.Parse(str[^1].ToString());returnfirst*10+last;// this converts first digit to a 10th digit e.g 1 = 10, 2 = 20}intChallenge_1(){string[]lines=File.ReadAllLines(FILE_PATH);returnlines.Select(OnlyNumerics).Select(GetFirstAndLast).Sum();}intChallenge_2(){string[]lines=File.ReadAllLines(FILE_PATH);string[]replaced=lines.Select(ReplaceNumbers).ToArray();returnreplaced.Select(OnlyNumerics).Select(GetFirstAndLast).Sum();}Console.WriteLine("Part 1:"+Challenge_1().ToString());Console.WriteLine("Part 2:"+Challenge_2().ToString());
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse