C
C#12mo ago
AceChewy

Advent of Code Day 1

I'm not sure why my code isn't accepted even though I'm sure it works:
private int Sum()
{
int Overall = 0;

Console.WriteLine("Enter the calibration document. Enter an empty line to finish.");

string line;
while ((line = Console.ReadLine()) != "")
{
int firstDigit = 0;
int lastDigit = 0;

// Find the first digit
for (int i = 0; i < line.Length; i++)
{
if (Char.IsDigit(line[i]))
{
firstDigit = Int32.Parse(line[i].ToString());
break;
}
}

// Find the last digit
for (int i = line.Length - 1; i >= 0; i--)
{
if (Char.IsDigit(line[i]))
{
lastDigit = Int32.Parse(line[i].ToString());
break;
}
}

Overall += firstDigit + lastDigit;
}

return Overall;
}
private int Sum()
{
int Overall = 0;

Console.WriteLine("Enter the calibration document. Enter an empty line to finish.");

string line;
while ((line = Console.ReadLine()) != "")
{
int firstDigit = 0;
int lastDigit = 0;

// Find the first digit
for (int i = 0; i < line.Length; i++)
{
if (Char.IsDigit(line[i]))
{
firstDigit = Int32.Parse(line[i].ToString());
break;
}
}

// Find the last digit
for (int i = line.Length - 1; i >= 0; i--)
{
if (Char.IsDigit(line[i]))
{
lastDigit = Int32.Parse(line[i].ToString());
break;
}
}

Overall += firstDigit + lastDigit;
}

return Overall;
}
7 Replies
Kiel
Kiel12mo ago
you're adding the numbers together wrong. if your first digit is 5 and your last digit is 7, you should be adding 57, not 12 Overall += firstDigit + lastDigit; is the problme line
AceChewy
AceChewyOP12mo ago
Right, that completely went over my head, I need to concatentate them first then parse them
Kiel
Kiel12mo ago
alternatively, you can multiply the first digit by 10
AceChewy
AceChewyOP12mo ago
so firstDigit *10 + lastDigit (which is also more efficient)
Kiel
Kiel12mo ago
blobthumbsup
AceChewy
AceChewyOP12mo ago
Thanks :) I'm also a little unsure why this doesn't work :
int Overall = 0;

var map = new Dictionary<string, int>
{
{ "0" , 0 },
{ "one" , 1 },
{ "two" , 2 },
{ "three", 3 },
{ "four" , 4 },
{ "five" , 5 },
{ "six" , 6 },
{ "seven", 7 },
{ "eight", 8 },
{ "nine" , 9 },
{ "1" , 1 },
{ "2" , 2 },
{ "3" , 3 },
{ "4" , 4 },
{ "5" , 5 },
{ "6" , 6 },
{ "7" , 7 },
{ "8" , 8 },
{ "9" , 9 },
};

Console.WriteLine("Enter the calibration document. Enter an empty line to finish.");

string line;
while ((line = Console.ReadLine()) != "")
{
string firstDigit = "";
string lastDigit = "";

// Find the first digit
for (int i = 0; i < line.Length; i++)
{
if (map.ContainsKey(line[i].ToString()))
{
firstDigit = line[i].ToString();
break;
}
}

// Find the last digit
for (int i = line.Length - 1; i >= 0; i--)
{
if (map.ContainsKey(line[i].ToString()))
{
lastDigit = line[i].ToString();
break;
}
}

Overall += map[firstDigit] * 10 + map[lastDigit];
}

Console.WriteLine(Overall);
int Overall = 0;

var map = new Dictionary<string, int>
{
{ "0" , 0 },
{ "one" , 1 },
{ "two" , 2 },
{ "three", 3 },
{ "four" , 4 },
{ "five" , 5 },
{ "six" , 6 },
{ "seven", 7 },
{ "eight", 8 },
{ "nine" , 9 },
{ "1" , 1 },
{ "2" , 2 },
{ "3" , 3 },
{ "4" , 4 },
{ "5" , 5 },
{ "6" , 6 },
{ "7" , 7 },
{ "8" , 8 },
{ "9" , 9 },
};

Console.WriteLine("Enter the calibration document. Enter an empty line to finish.");

string line;
while ((line = Console.ReadLine()) != "")
{
string firstDigit = "";
string lastDigit = "";

// Find the first digit
for (int i = 0; i < line.Length; i++)
{
if (map.ContainsKey(line[i].ToString()))
{
firstDigit = line[i].ToString();
break;
}
}

// Find the last digit
for (int i = line.Length - 1; i >= 0; i--)
{
if (map.ContainsKey(line[i].ToString()))
{
lastDigit = line[i].ToString();
break;
}
}

Overall += map[firstDigit] * 10 + map[lastDigit];
}

Console.WriteLine(Overall);
It's late here now so I'll try again later
Kiel
Kiel12mo ago
you're only checking a single character in the string, so it will never match on the "word" numbers you'd need to rework the logic that loops from end to beginning to be aware of characters after it
Want results from more Discord servers?
Add your server