From 3a3ccc7a5ff5831eb4c659924ee0b93c138cebac Mon Sep 17 00:00:00 2001 From: Alain Kaiser Date: Fri, 1 Dec 2023 13:22:21 +0100 Subject: [PATCH] fix: problem 2 types --- AdventOfCode/Day01.cs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/AdventOfCode/Day01.cs b/AdventOfCode/Day01.cs index 800aefa..998bfe2 100644 --- a/AdventOfCode/Day01.cs +++ b/AdventOfCode/Day01.cs @@ -1,6 +1,4 @@ -using System.Text.RegularExpressions; - -namespace AdventOfCode; +namespace AdventOfCode; public sealed class Day01 : BaseDay { @@ -47,47 +45,45 @@ public override ValueTask 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(sum.ToString());