✅ List gets weird numbers
I was hoping for this to take the first int and the last int, yes there are probably better ways to do this but I am new to c# and want to come up with my own solutions.
Atm if I run this code then "ints[0]" comes out as 57 (should be 9)
and "ints[1]" comes out as 49 (should be 1)
my idea was that it first uses the Add function to add 2 indexes (0 = the first number)
then if it found more numbers then 2 if would just replace the last index (1).
If anyone know why the values come out as they do, any help would be much appreciated
7 Replies
Firstly: we have an #advent-of-code if you want more specific help with AoC chars are kinda weird, because they're actually ints. When you do
ints.Add(c)
, what it does is that it converts the char into the numerical code for the character, not the digit represented by the character.Thinker
REPL Result: Success
Result: int
Compile: 202.277ms | Execution: 21.727ms | React with ❌ to remove this embed.
There are two things you can do here: you can use
char.GetNumericValue(c)
to get the numerical value of the character, or you can do the slightly hackier thing of c - '0'
. Since '0'
is actually an integer, you can subtract it from any other character to get the actual numeric value of the character, since if you subtract '0'
from '0'
then you'll get 0, '0'
from '1'
you'll get 1, and so on.int
and char
are not entirely the same
oops, was scrolled up
Thinker said what I wanted to sayAlright, that makes sense, ty for the help, is there a way to put the post as solved, or do I just leave it there?
$close
Use the /close command to mark a forum thread as answered