about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.editorconfig4
-rw-r--r--Day2.cs18
2 files changed, 14 insertions, 8 deletions
diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..eb9e412
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,4 @@
+[*.cs]
+
+# CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive).
+dotnet_diagnostic.CS8509.severity = suggestion
diff --git a/Day2.cs b/Day2.cs
index 095b454..1549e28 100644
--- a/Day2.cs
+++ b/Day2.cs
@@ -11,29 +11,31 @@ namespace aoc2019
         private readonly IEnumerable<int> input =
             File.ReadLines("input/day2.in").First().Split(',').Select(int.Parse);
 
-        public static List<int> RunIntCode(int noun, int verb, List<int> v)
+        public int RunIntCode(int noun, int verb)
         {
+            var v = input.ToList();
             v[1] = noun; v[2] = verb;
 
             for (var i = 0; v[i] != 99; i += 4)
-                switch (v[i])
+                v[v[i + 3]] = v[i] switch
                 {
-                    case 1: v[v[i + 3]] = v[v[i + 1]] + v[v[i + 2]]; break;
-                    case 2: v[v[i + 3]] = v[v[i + 1]] * v[v[i + 2]]; break;
-                }
-            return v;
+                    1 => v[v[i + 1]] + v[v[i + 2]],
+                    2 => v[v[i + 1]] * v[v[i + 2]]
+                };
+
+            return v[0];
         }
 
         public override string Part1()
         {
-            return $"{RunIntCode(12, 2, input.ToList())[0]}";
+            return $"{RunIntCode(12, 2)}";
         }
 
         public override string Part2()
         {
             for (var i = 0; i < 100; i++)
                 for (var j = 0; j < 100; j++)
-                    if (RunIntCode(i, j, input.ToList())[0] == 19690720)
+                    if (RunIntCode(i, j) == 19690720)
                         return $"{100 * i + j}";
 
             return string.Empty;