C
C#7mo ago
AdiZ

✅ 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.
string input = """
9sixsevenz3
seven1cvdvnhpgthfhfljmnq
6tvxlgrsevenjvbxbfqrsk4seven
9zml
""";

int sum = 0;
foreach(string word in input.Split())
{
List<int> nums = [];
foreach(char ch in word)
{
if(Char.IsDigit(ch))
{
nums.Add(Convert.ToInt32(ch - '0'));
}
}

int firstNumber = nums[0];
int lastNumber = nums[^1];
int fullNumber = firstNumber * 10 + lastNumber;
sum += fullNumber;
}

Console.WriteLine(sum);
string input = """
9sixsevenz3
seven1cvdvnhpgthfhfljmnq
6tvxlgrsevenjvbxbfqrsk4seven
9zml
""";

int sum = 0;
foreach(string word in input.Split())
{
List<int> nums = [];
foreach(char ch in word)
{
if(Char.IsDigit(ch))
{
nums.Add(Convert.ToInt32(ch - '0'));
}
}

int firstNumber = nums[0];
int lastNumber = nums[^1];
int fullNumber = firstNumber * 10 + lastNumber;
sum += fullNumber;
}

Console.WriteLine(sum);
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
AdiZ
AdiZ7mo ago
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.
MODiX
MODiX7mo ago
AdiZ
REPL Result: Failure
string input = """
9sixsevenz3
seven1cvdvnhpgthfhfljmnq
6tvxlgrsevenjvbxbfqrsk4seven
9zml
""";

int sum = 0;
foreach(string word in input.Split())
{
List<int> nums = [];
foreach(char ch in word)
{
if(Char.IsDigit(ch))
{
nums.Add(Convert.ToInt32(ch - '0'));
}
}

int firstNumber = nums[0];
int lastNumber = nums[^1];
int fullNumber = firstNumber * 10 + lastNumber;
sum += fullNumber;
}

Console.WriteLine(sum);
string input = """
9sixsevenz3
seven1cvdvnhpgthfhfljmnq
6tvxlgrsevenjvbxbfqrsk4seven
9zml
""";

int sum = 0;
foreach(string word in input.Split())
{
List<int> nums = [];
foreach(char ch in word)
{
if(Char.IsDigit(ch))
{
nums.Add(Convert.ToInt32(ch - '0'));
}
}

int firstNumber = nums[0];
int lastNumber = nums[^1];
int fullNumber = firstNumber * 10 + lastNumber;
sum += fullNumber;
}

Console.WriteLine(sum);
Exception: CompilationErrorException
- Invalid expression term '['
- Syntax error; value expected
- Invalid expression term '['
- Syntax error; value expected
Compile: 608.932ms | Execution: 0.000ms | React with ❌ to remove this embed.
MODiX
MODiX7mo ago
AdiZ
REPL Result: Failure
List<int> lst = [1,2,3]; Console.WriteLine(lst[2]);
List<int> lst = [1,2,3]; Console.WriteLine(lst[2]);
Exception: CompilationErrorException
- Invalid expression term '['
- Invalid expression term '['
Compile: 445.149ms | Execution: 0.000ms | React with ❌ to remove this embed.
AdiZ
AdiZ7mo ago
Ohhhh But why? That should work? Running this:
string input = """
9sixsevenz3
seven1cvdvnhpgthfhfljmnq
6tvxlgrsevenjvbxbfqrsk4seven
9zml
""";

int sum = 0;
foreach(string word in input.Split())
{
List<int> nums = new();
foreach(char ch in word)
{
if(Char.IsDigit(ch))
{
nums.Add(Convert.ToInt32(ch - '0'));
}
}

int firstNumber = nums[0];
int lastNumber = nums[^1];
int fullNumber = firstNumber * 10 + lastNumber;
sum += fullNumber;

foreach(int num in nums)
{
Console.WriteLine(num);
}
}
string input = """
9sixsevenz3
seven1cvdvnhpgthfhfljmnq
6tvxlgrsevenjvbxbfqrsk4seven
9zml
""";

int sum = 0;
foreach(string word in input.Split())
{
List<int> nums = new();
foreach(char ch in word)
{
if(Char.IsDigit(ch))
{
nums.Add(Convert.ToInt32(ch - '0'));
}
}

int firstNumber = nums[0];
int lastNumber = nums[^1];
int fullNumber = firstNumber * 10 + lastNumber;
sum += fullNumber;

foreach(int num in nums)
{
Console.WriteLine(num);
}
}
Prints 9 and 3 but then fails on trying to print the 1 in the second line. I'm so confused.
Joschi
Joschi7mo ago
Right at the start of your loop. Insert a Console.WriteLine(word) and take a look what it outputs
TheRanger
TheRanger7mo ago
bot is still on .net 7
AdiZ
AdiZ7mo ago
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.
Joschi
Joschi7mo ago
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
string input = """
9sixsevenz3
seven1cvdvnhpgthfhfljmnq
6tvxlgrsevenjvbxbfqrsk4seven
9zml
""";

int sum = 0;
foreach(string word in input.Split())
{
Console.WriteLine("The Word is: " + word);
List<int> nums = new();
foreach(char ch in word)
{
if(Char.IsDigit(ch))
{
nums.Add(Convert.ToInt32(ch - '0'));
}
}

int firstNumber = nums[0];
int lastNumber = nums[^1];
int fullNumber = firstNumber * 10 + lastNumber;
sum += fullNumber;

Console.WriteLine("The Nums are:");
foreach(int num in nums)
{
Console.WriteLine(num);
}
}
string input = """
9sixsevenz3
seven1cvdvnhpgthfhfljmnq
6tvxlgrsevenjvbxbfqrsk4seven
9zml
""";

int sum = 0;
foreach(string word in input.Split())
{
Console.WriteLine("The Word is: " + word);
List<int> nums = new();
foreach(char ch in word)
{
if(Char.IsDigit(ch))
{
nums.Add(Convert.ToInt32(ch - '0'));
}
}

int firstNumber = nums[0];
int lastNumber = nums[^1];
int fullNumber = firstNumber * 10 + lastNumber;
sum += fullNumber;

Console.WriteLine("The Nums are:");
foreach(int num in nums)
{
Console.WriteLine(num);
}
}
Returns
The Word is: 9sixsevenz3
The Nums are:
9
3
The Word is: seven1cvdvnhpgthfhfljmnq
The Nums are:
1
The Word is: 6tvxlgrsevenjvbxbfqrsk4seven
The Nums are:
6
4
The Word is: 9zml
The Nums are:
9
The Word is: 9sixsevenz3
The Nums are:
9
3
The Word is: seven1cvdvnhpgthfhfljmnq
The Nums are:
1
The Word is: 6tvxlgrsevenjvbxbfqrsk4seven
The Nums are:
6
4
The Word is: 9zml
The Nums are:
9
TheRanger
TheRanger7mo ago
sounds right to me
AdiZ
AdiZ7mo ago
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
AdiZ
AdiZ7mo ago
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.
AdiZ
AdiZ7mo ago
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:
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')'
System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')'
And VS is telling me that the error happens on line 1018 Which is this:
int firstNumber = nums[0];
int firstNumber = nums[0];
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 it
Joschi
Joschi7mo ago
Which string does he parse while it crashes? Maybe there are strings that do not contain numbers
AdiZ
AdiZ7mo ago
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?
Joschi
Joschi7mo ago
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 crash
AdiZ
AdiZ7mo ago
Then I don't think it would fail on the second line... Wait two secs
MODiX
MODiX7mo ago
AdiZ
REPL Result: Success
List<int> list = new() {1};
Console.WriteLine(list[0]);
Console.WriteLine(list[^1]);
List<int> list = new() {1};
Console.WriteLine(list[0]);
Console.WriteLine(list[^1]);
Console Output
1
1
1
1
Compile: 505.699ms | Execution: 34.891ms | React with ❌ to remove this embed.
AdiZ
AdiZ7mo ago
Oh I thought that might have been the issue A list of one element.
Joschi
Joschi7mo ago
this line will crash if your list does not contain any elements with this exact error
AdiZ
AdiZ7mo ago
Aha
if(nums.Count == 0)
{
continue;
}
int firstNumber = nums[0];
int lastNumber = nums[^1];
int fullNumber = firstNumber * 10 + lastNumber;
sum += fullNumber;
if(nums.Count == 0)
{
continue;
}
int firstNumber = nums[0];
int lastNumber = nums[^1];
int fullNumber = firstNumber * 10 + lastNumber;
sum += fullNumber;
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 🤦
Joschi
Joschi7mo ago
That should not be possible with your code, but well if it works it works 😄
AdiZ
AdiZ7mo ago
I won't fix what's not broken 😂 Onto part two - thank you for all the help and quick responses!
Joschi
Joschi7mo ago
Good luck with the second part!
AdiZ
AdiZ7mo ago
Thanks 🙂