SylvainDe / aoc2021

Advent Of Code 2021
MIT License
0 stars 0 forks source link

Day 3 throw an exception with some input sets #1

Closed dreherch closed 2 years ago

dreherch commented 2 years ago

Hi,

I was trying to do the advent of code without actually coding, and I got stuck on day 3, as your code threw an exception with my input. It's really annoying, because I had to read you code and what we are asked to guess I could work around the issue with the following patch

diff --git a/day3.py b/day3.py
index 4f50594..5fa0b18 100644
--- a/day3.py
+++ b/day3.py
@@ -23,10 +23,13 @@ def get_power_consumption(diagnostic):

 def oxygen_rating(diagnostic, i):
     c = collections.Counter(diag[i] for diag in diagnostic)
-    (val1, nb1), (val2, nb2) = c.most_common(2)
-    if nb1 == nb2:
-        return "1"
-    return val1
+    try:
+        (val1, nb1), (val2, nb2) = c.most_common(2)
+        if nb1 == nb2:
+            return "1"
+        return val1
+    except ValueError:
+        return "0"

 def co2_rating(diagnostic, i):

Please also test your code with input files you don't have for the coming days.

thx

SylvainDe commented 2 years ago

Issue mentionned fixed with https://github.com/SylvainDe/aoc2021/commit/c9f76134b6cee18dea2381ff004b634b4294baea#diff-ea372b7f0d693d21e1ce76fcd9a6de29254513319f3e5be6c8df1a0eedfb00d7 . I suspect invalid inputs can still fail in many different ways.