Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Advent Of Code 2023: Day 1 - Trebuchet
Grant Riordan
Grant Riordan

Posted on

     

Advent Of Code 2023: Day 1 - Trebuchet

Day 1 - Trebuchet

link

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());
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Lvl 11 - Lead Software Engineer & Lego FanaticTwitter link below 👇
  • Location
    Yorkshire, England
  • Education
    BSc(Hons) in Applied Computing
  • Work
    Application Development Specialist
  • Joined

More fromGrant Riordan

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp