This is just math, but configured a little oddly.
long total = 0;
for (var i = 0; i < lines[0].Length; i++)
{
long sum = int.Parse(lines[0][i]);
if (lines.Last()[i] == "+")
{
for (var j = 1; j < lines.Count - 1; j++)
{
sum += int.Parse(lines[j][i]);
}
}
else
{
for (var j = 1; j < lines.Count - 1; j++)
{
sum *= int.Parse(lines[j][i]);
}
}
total += sum;
}
It feels a bit clumsy to basically just repeat the main section twice, just swapping the operation, but it runs quickly and is simple enough to read.
Ahhh, okay. It's simple because it's really a parsing problem. Numbers are vertical, not horizontal. Wild. It makes more sense to parse this the same time as doing the operations.
char op = '+';
long total = 0, sum = 0;
for (int col = 0; col < input[0].Length; col++)
{
// Base case - starting new problem, so record last
if (input.Last()[col] != ' ')
{
total += sum;
sum = 0;
op = input.Last()[col];
}
// Parse digits of column
var cnum = 0;
for (int row = 0; row < input.Length - 1; row++)
{
if (Utils.IsDigit(input[row][col]))
{
cnum = (cnum * 10) + (input[row][col] - '0');
}
}
// Set first number, skip blank column, or do operation
if (sum == 0) sum = cnum;
else if (cnum == 0) continue;
else sum = op == '+' ? sum + cnum : sum * cnum;
}