✅ Advent of Code Day 1 - Issue with Chars
For this problem, you essentially have to find the first and last digit contained in each line, so for a line
271lonepxp2flbmbz
the first digit would be 2
and the last digit 2
as well. Put 'em together, you get 22
. Now you have to find that value for each of the 1000 lines provided and add them all together.
This is my code. However, I just keep getting an Index Out of Range
error on the line that says
int firstNumber = nums[0];
and I just don't get why. I originally had the code a lot more condensed but I expanded it to debug and that appears to be where the issue is. Naturally I initially thought that nothing was being added to the array, but so far I can't figure out why that would be. I'm going to go and Console.WriteLine
nums
right now to see if it actually contains anything, but either way I'd appreciate it if someone could help me see what I did wrong here.24 Replies
Please ping me if you reply, thanks!
Yeah,
nums
does appear to have numbers being added to it, so it just doesn't make sense to me.AdiZ
REPL Result: Failure
Exception: CompilationErrorException
Compile: 608.932ms | Execution: 0.000ms | React with ❌ to remove this embed.
AdiZ
REPL Result: Failure
Exception: CompilationErrorException
Compile: 445.149ms | Execution: 0.000ms | React with ❌ to remove this embed.
Ohhhh
But why? That should work?
Running this:
Prints
9
and 3
but then fails on trying to print the 1
in the second line.
I'm so confused.Right at the start of your loop.
Insert a Console.WriteLine(word) and take a look what it outputs
bot is still on .net 7
Not at the PC right now, but I remember running a for each loop that literally just printed the word and that worked fine
Good to know, thanks.
Huh ok yeah, I actually did not know that String.Split defaults to a new line as delimiter.
But your code works on my end
Returns
sounds right to me
That's so irritating
It just fails on my PC. Other code works fine though... When I get back I'll try again and let you know. Thanks @Joschi!
@Joschi something definitely isn't working here
Pastebin
using System.Diagnostics.Tracing;string input = """9sixsevenz3seven...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
This is my code (sorry it's so long, I just want the entire thing there in case something's wrong with it
And after executing the loop on the first word, I get this error:
And VS is telling me that the error happens on line
1018
Which is this:
If you could test that entire code for me and let me know if it's still working on your machine and not mine that'd be great
Because I just don't get itWhich string does he parse while it crashes?
Maybe there are strings that do not contain numbers
The only conversion happening is on chars that have already been deemed to contain a digit
And
Convert.ToInt32()
would fail then, not the line that is failing right now.
Right?Your code assumes that there is at least one element in
nums
.
If your input doesn't contain any numbers on some line, it will crash
If you try a test like input = "hello"
you should see it crashThen I don't think it would fail on the second line...
Wait two secs
AdiZ
REPL Result: Success
Console Output
Compile: 505.699ms | Execution: 34.891ms | React with ❌ to remove this embed.
Oh
I thought that might have been the issue
A list of one element.
this line will crash if your list does not contain any elements
with this exact error
Aha
Changed it to this
And it ran fine!
And it was correct! Amazing.
I just don't get why it was crashing on the second word, which so clearly had numbers in it.
Come to think of it, when I was debugging, it said that
nums
was null
, how did I not realize what that meant 🤦That should not be possible with your code, but well if it works it works 😄
I won't fix what's not broken 😂
Onto part two - thank you for all the help and quick responses!
Good luck with the second part!
Thanks 🙂