about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--aoc2021.test/DayTests.cs2
-rw-r--r--aoc2021/Day03.cs23
2 files changed, 23 insertions, 2 deletions
diff --git a/aoc2021.test/DayTests.cs b/aoc2021.test/DayTests.cs
index f9d2a1d..df97b57 100644
--- a/aoc2021.test/DayTests.cs
+++ b/aoc2021.test/DayTests.cs
@@ -6,7 +6,7 @@ public class DayTests
     [DataTestMethod]
     [DataRow(typeof(Day01), "1616", "1645")]
     [DataRow(typeof(Day02), "2272262", "2134882034")]
-    [DataRow(typeof(Day03), "3009600", "")]
+    [DataRow(typeof(Day03), "3009600", "6940518")]
     public void CheckAllDays(Type dayType, string part1, string part2)
     {
         var s = Stopwatch.StartNew();
diff --git a/aoc2021/Day03.cs b/aoc2021/Day03.cs
index a9e5c38..e1003c4 100644
--- a/aoc2021/Day03.cs
+++ b/aoc2021/Day03.cs
@@ -33,6 +33,27 @@ public sealed class Day03 : Day
 
     public override string Part2()
     {
-        return "";
+        var o = _report;
+        var c = _report;
+        
+        var i = 0;
+        while (o.Count > 1)
+        {
+            var most = o.Count(r => r[i] == '1') >= o.Count / 2.0 ? '1' : '0';
+            o = o.Where(r => r[i] == most).ToList();
+            i++;
+        }
+        var o2 = Convert.ToInt64(o.Single(), 2);
+
+        i = 0;
+        while (c.Count > 1)
+        {
+            var most = c.Count(r => r[i] == '1') >= c.Count / 2.0 ? '0' : '1';
+            c = c.Where(r => r[i] == most).ToList();
+            i++;
+        }
+        var co2 = Convert.ToInt64(c.Single(), 2);
+
+        return $"{o2 * co2}";
     }
 }