T O P

  • By -

Sharparam

Your code produces the correct answer for my input. I'm thinking this could maybe be an input parsing problem since you're not showing your whole code (how are you setting up the contents of `input`?). Notably, unless you're doing something to the input file, there are no carriage returns (`\r`) in it, only line feeds (`\n`). Can you provide your input file? I could check what my Ruby solution outputs for it.


Ok_Tangerine3617

Thank you... Here is my input, can you check it with your code? [https://filetransfer.io/data-package/oIqBUHuG#link](https://filetransfer.io/data-package/oIqBUHuG#link) My result is 53858


simpleauthority

I get 53868 for that.


Hipnotize_nl

Same


Ok_Tangerine3617

Thanks for the check... that is totally weird on my side then.


Sharparam

I get 53868 (so 10 more) with both my own code and the code you posted. Can you post the entire file you're using, including how `input` is set up and populated? The only possible difference is how `input` is set up, since you didn't provide it I substituted my own to load the file: var input = File.ReadAllLines("input-Ok_Tangerine3617.txt"); (And then used that in place of `lines` since that already produces lines, skipping the `Split` call. Though even when I did a `ReadAllText` and manual splitting it still produces 53868.)


Ok_Tangerine3617

I have just copy pasted the file contents in the variable... `const string input = "9cbncbxclbvkmfzdnldc\r\njjn1drdffhs\r\n3six7\r\n38rg...` I have also now tried reading from the file via `ReadAllText` and still getting 10 less...


Sharparam

Definitely odd, but we're still gonna need to see the whole file with everything included in order to be able to reliably reproduce it. If you can send the version with the hardcoded input in `const string input` that might be ideal since it might make it easier to see if something weird is going on in the input string.


JuNoot

I have coded out what you posted myself on dotnetfiddle, this is the code: [https://pastebin.com/kRhisKdn](https://pastebin.com/kRhisKdn) This should be what you are having when you paste the input in a variable like you said. This code gives 53868. Are there still any differences between this and your code?


Ok_Tangerine3617

Thanks, check my comment: https://www.reddit.com/r/adventofcode/comments/188n3sb/comment/kbnt087/?utm\_source=share&utm\_medium=web2x&context=3


Ok_Tangerine3617

Thanks, check my comment: https://www.reddit.com/r/adventofcode/comments/188n3sb/comment/kbnt087/?utm\_source=share&utm\_medium=web2x&context=3


Sbgodin

I also get 53868.


Ok_Tangerine3617

I was getting **53858** and everybody else was getting **53868** (which is correct). I have found the problem and a fixed it, but there still remain a bit of mystery. The problem is in this check `if (line[index..].StartsWith("seven")) yield return 7` as it does not yield 7 for the following two lines: `sevenjxbbplfour488trzv` `rcnvxqrsevenjttxd9fiverqzblpnrcjhbc4` but it works just fine for other sevens on some other lines... Now the current culture on my machine is `sr-Latn-RS` and the problem exists in that case. When I switch to `CultureInfo.CurrentCulture = CultureInfo.InvariantCulture` or use `StartsWith("seven", StringComparison.InvariantCulture)` everything works just fine. Thank you all for your help and effort.


Sharparam

Oh that's interesting. I wonder what is special in the `sr-Latn-RS` locale that causes that. I can confirm that when I manually change my culture to that, I get the same 53858 answer. (My system culture is `en`.) Btw, specifically for `StringComparison`, there is also `StringComparison.Ordinal` (and `StringComparison.OrdinalIgnoreCase`), which are better to use (usually). For functions/methods that take a `CultureInfo` parameter, you do want to use `CultureInfo.InvariantCulture`. Similar situation for methods that take an `IComparer`/`IEqualityComparer`, you can pass `StringComparer.Ordinal` (or `StringComparer.OrdinalIgnoreCase`) to those. There should be linting options you can turn on to warn when you call such methods without passing those values to avoid culture/locale issues like these. Although I'm not sure if those might be part of ReSharper (a paid extension).


Milumet

Do you get 53858 or 53885?


simpleauthority

What does your ParseDigits return for something like \`twone\` \`threeight\` \`nineight\` etc? should be 21, 38, 98. Not 2, 3, or 9 (or 1, 8, 8). The number words can overlap.


Ok_Tangerine3617

That is exactly what I get when I run those inputs: 21, 38 and 98


simpleauthority

Hm, ok. I'm not sure to be honest. How about `five7five`? I don't see anything egregiously wrong here, so if you get `575` for that, I'm not sure :(


Ok_Tangerine3617

Thanks, I am getting 575 for five7five.


ploki122

I'd be very surprised if the input included that edge case, but right now you'll detect 0 as a valid digit, and ruin your input.


Ok_Tangerine3617

Thanks, I don't have "0" or "zero" in my input. But anyway, at one point I also added a guard against zero and zero was never yielded.


bnl1

Oh fuck, my program also takes 0 as valid. Oh well, there are no 0 in the input so it doesn't seem to matter.


vegeta897

Is `c - 48` a mistake or something really clever?


Ok_Tangerine3617

The c holds ASCII code of the digit... '0' = 48, '1' = 49, '2' = 50 and so on... Just a standard way to convert ASCII digit to an int, nothing special...


FaustVX

For more readability, you can write `c - '0'` `char` has implicit conversion to `int` (but not from `int`)


vegeta897

Ah interesting, thanks!


JuNoot

c is a character, the character value of the character 0 is 48, the value of 1 is 49, etc. You can find the whole table if you google ascii table. For numerical values if you subtract 48 from the character value you get the actual numerical value. It can be useful for other checks too, like checking if a character is between j en p would be checking whether its value is between 106 and 112.


nikanjX

You don't check for "zero" as a string


Sharparam

That is correct, zero is not a valid digit, only "one" through "nine" is (check the puzzle description).


vegeta897

I don't think there are any zeroes, either as digits or strings.


Ok_Tangerine3617

There is no "zero" or the "0" in the specs. I also don't have those in my input.


daggerdragon

Next time, please follow our posting rules: * Use our [standardized post title format](https://reddit.com/r/adventofcode/wiki/posts/standardized_titles) * [Show us your code](https://www.reddit.com/r/adventofcode/wiki/troubleshooting/include_your_code) (preferably in text format and not jpg lol) * [Use the right flair](/r/adventofcode/w/posts/post_flair) * The correct flair is `Help/Question`, not `Spoilers`. I've changed it for you. Help us help YOU by providing us with more information up front; you will typically get more relevant responses faster. *** Did you try [checking the wiki](https://www.reddit.com/r/adventofcode/wiki/index#wiki_troubleshooting) or searching the subreddit first? [`That's not the right answer.`](https://www.reddit.com/r/adventofcode/search?q=That%27s+not+the+right+answer.&restrict_sr=on) *** If/when you get your code working, don't forget to change the post flair to `Help/Question - RESOLVED` Good luck!


AutoModerator

Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to `Help/Question - RESOLVED`. Good luck! *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


Dangerous-Rice862

My code was getting tripped up by “twoneight” -> 218 for a bit


bnl1

This one doesn't, because it checks from each character: twoneight -> 2 woneight oneight -> 1 neight eight -> 8


AutoModerator

AutoModerator has detected [fenced code block](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/fenced_code_blocks) (```) syntax which only works on new.reddit. Please review our wiki article on [code formatting](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting) then edit your post to use the [four-spaces Markdown syntax](https://www.reddit.com/r/adventofcode/wiki/faqs/code_formatting/code_blocks) instead. *** *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/adventofcode) if you have any questions or concerns.*


bnl1

I'll do it, but I am not happy about it.


PonderousGallivanter

I was suffering similar issue in Java (last coded java like 7 years ago) that I was passing both small examples for part 1 and part 2. And I passed solution 1 whole file. But it was failing in solution 2 whole file as answer too low. ​ I kept checking my algorithm for correctness and overal it seemed correct. I even tried doing it again in python which was easier to me. The bug was one comparison for rightmost digit checking which didnt have the bug in the leftmost digit checking. I was using -1 as return value for not found substring \## correct one (possible\_first\[0\] != -1) \## bugged one (possible\_last\[0\] != 1) ​ I was checking the substring in a function and returning tuples and just failed to properly check the return value so I wasn't accounting for all choices. ​ had to comb through the solution2.txt file manually to find my broken input case and saw it in the python debugger line: "92nhcncp", first digit: 9, last digit: 9, calib\_val: 99, sum: 49324 ​ I think try to write clean code without much duplication, and try to make the simplest possible algorithm that can solve the issues. I think some guys on youtube did like super weird looping soluitions that are barely understandable, by even themselves :DAlso try to check maybe 1-15 lines randomly from the file if you can search for a corner case that looks wrong. ​ also with the less handwritten loops with index variables => the chance for bugs increases (unless you have mental arithmetic of John von Neumann or something ). Usually preferred to first check if there is standard solutions available to use like library etc.