Use switch statement

I keep forgetting these exists and they're great.
This commit is contained in:
2021-12-16 16:06:45 -06:00
parent 13cc275842
commit c48770c388

View File

@ -119,46 +119,18 @@ internal class Day16 : Day
done = done || (totalLength != 0 && lengthProcessed == totalLength); done = done || (totalLength != 0 && lengthProcessed == totalLength);
} }
long result = 0; var result = inType switch
switch (inType)
{ {
case 0: 0 => operands.Sum(),
result = operands.Sum(x => x); 1 => operands.Aggregate(1L, (agg, x) => x * agg),
break; 2 => operands.Min(),
3 => operands.Max(),
case 1: 4 => throw new Exception(),
result = operands.Aggregate(1L, (agg, x) => x * agg); 5 => operands[0] > operands[1] ? 1 : 0,
break; 6 => operands[0] < operands[1] ? 1 : 0,
7 => operands[0] == operands[1] ? 1 : 0,
case 2: _ => throw new Exception(),
result = operands.Min(x => x); };
break;
case 3:
result = operands.Max(x => x);
break;
case 4:
throw new Exception();
case 5:
System.Diagnostics.Debug.Assert(operands.Count == 2);
result = operands[0] > operands[1] ? 1 : 0;
break;
case 6:
System.Diagnostics.Debug.Assert(operands.Count == 2);
result = operands[0] < operands[1] ? 1 : 0;
break;
case 7:
System.Diagnostics.Debug.Assert(operands.Count == 2);
result = operands[0] == operands[1] ? 1 : 0;
break;
default:
throw new Exception();
}
return (versionTotal, result); return (versionTotal, result);
} }