improved time complexity from exponential to linear time

This commit is contained in:
2025-12-03 23:04:27 -05:00
parent e93bf9ecd7
commit 1214dcadfe

View File

@@ -11,17 +11,23 @@ public class Lobby : IAdventSolution
var total = 0;
foreach (var line in lines)
{
var max = 0;
var first = '0';
var second = '0';
for (var i = 0; i < line.Length; i++)
{
for (var j = i + 1; j < line.Length; j++)
if (i != line.Length - 1 && line[i] > first)
{
var num = int.Parse(line[i].ToString() + line[j].ToString());
if (num > max) max = num;
first = line[i];
second = line[i + 1];
}
else if (line[i] > second)
{
second = line[i];
}
}
}
}
total += max;
total += int.Parse($"{first}{second}");
}
return new AdventSolution(total.ToString(), null);