From c48770c388c5c20bd4f36c5a61d6c48a843374e4 Mon Sep 17 00:00:00 2001 From: Parnic Date: Thu, 16 Dec 2021 16:06:45 -0600 Subject: [PATCH] Use switch statement I keep forgetting these exists and they're great. --- src/16.cs | 50 +++++++++++--------------------------------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/src/16.cs b/src/16.cs index b876365..3f09ad7 100644 --- a/src/16.cs +++ b/src/16.cs @@ -119,46 +119,18 @@ internal class Day16 : Day done = done || (totalLength != 0 && lengthProcessed == totalLength); } - long result = 0; - switch (inType) + var result = inType switch { - case 0: - result = operands.Sum(x => x); - break; - - case 1: - result = operands.Aggregate(1L, (agg, x) => x * agg); - break; - - case 2: - 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(); - } + 0 => operands.Sum(), + 1 => operands.Aggregate(1L, (agg, x) => x * agg), + 2 => operands.Min(), + 3 => operands.Max(), + 4 => throw new Exception(), + 5 => operands[0] > operands[1] ? 1 : 0, + 6 => operands[0] < operands[1] ? 1 : 0, + 7 => operands[0] == operands[1] ? 1 : 0, + _ => throw new Exception(), + }; return (versionTotal, result); }