Skip to content

Commit

Permalink
fix: problem 2 types
Browse files Browse the repository at this point in the history
  • Loading branch information
alainkaiser committed Dec 1, 2023
1 parent 56daf74 commit 3a3ccc7
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions AdventOfCode/Day01.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Text.RegularExpressions;

namespace AdventOfCode;
namespace AdventOfCode;

public sealed class Day01 : BaseDay
{
Expand Down Expand Up @@ -47,47 +45,45 @@ public override ValueTask<string> Solve_2()
{ "one", 1 }, { "two", 2 }, { "three", 3 }, { "four", 4 },
{ "five", 5 }, { "six", 6 }, { "seven", 7 }, { "eight", 8 }, { "nine", 9 }
};

int sum = 0;

foreach (var line in _input)
{
int? firstNumber = null;
int? lastNumber = null;
string firstNumber = null;
string lastNumber = null;

for (var i = 0; i < line.Length; i++)
{
foreach (var wordDigit in wordDigits)
{
if (line.Length - i >= wordDigit.Key.Length &&
line.Substring(i, wordDigit.Key.Length).Equals(wordDigit.Key, StringComparison.OrdinalIgnoreCase))
line.Substring(i, wordDigit.Key.Length)
.Equals(wordDigit.Key, StringComparison.OrdinalIgnoreCase))
{
if (!firstNumber.HasValue)
if (string.IsNullOrEmpty(firstNumber))
{
firstNumber = wordDigit.Value;
firstNumber = wordDigit.Value.ToString();
}
lastNumber = wordDigit.Value;

lastNumber = wordDigit.Value.ToString();
i += wordDigit.Key.Length - 1; // Skip the word digit
break;
}
}

if (!firstNumber.HasValue && char.IsDigit(line[i]))
if (string.IsNullOrEmpty(firstNumber) && char.IsDigit(line[i]))
{
firstNumber = line[i];
firstNumber = line[i].ToString();
}

if (char.IsDigit(line[i]))
{
lastNumber = line[i];
lastNumber = line[i].ToString();
}
}

if (firstNumber.HasValue && lastNumber.HasValue)
{
sum += int.Parse(firstNumber + lastNumber.ToString());
}
sum += int.Parse(firstNumber + lastNumber);
}

return new ValueTask<string>(sum.ToString());
Expand Down

0 comments on commit 3a3ccc7

Please sign in to comment.