]> git.friedersdorff.com Git - max/advent_of_code_2021.git/commitdiff
Do day 8
authorMaximilian Friedersdorff <max@friedersdorff.com>
Wed, 8 Dec 2021 21:33:52 +0000 (21:33 +0000)
committerMaximilian Friedersdorff <max@friedersdorff.com>
Wed, 8 Dec 2021 21:33:52 +0000 (21:33 +0000)
8_1.py [new file with mode: 0644]
8_2.py [new file with mode: 0644]

diff --git a/8_1.py b/8_1.py
new file mode 100644 (file)
index 0000000..b6e606d
--- /dev/null
+++ b/8_1.py
@@ -0,0 +1,11 @@
+"""Decode stupid 7 segment displays"""
+
+count = 0
+with open("8_input.txt") as f:
+    for line in f:
+        digits = line.split("|")[1].strip().split(" ")
+        for digit in digits:
+            if len(digit) in [2, 3, 4, 7]:  # Corresponds to 1, 7, 4, 8
+                count += 1
+
+print(count)
diff --git a/8_2.py b/8_2.py
new file mode 100644 (file)
index 0000000..dca645d
--- /dev/null
+++ b/8_2.py
@@ -0,0 +1,58 @@
+"""Decode stupid 7 segment displays"""
+
+s = 0
+with open("8_input.txt") as f:
+    for line in f:
+        mapping = {}
+
+        digits = [set(d) for d in line.split("|")[0].strip().split(" ")]
+        to_decode = [set(d) for d in line.split("|")[1].strip().split(" ")]
+        for digit_letters in digits:
+            if len(digit_letters) == 2:
+                mapping[1] = digit_letters
+            elif len(digit_letters) == 3:
+                mapping[7] = digit_letters
+            elif len(digit_letters) == 4:
+                mapping[4] = digit_letters
+            elif len(digit_letters) == 7:
+                mapping[8] = digit_letters
+
+        # Identify the 9, it's a strict superset of 4
+        for digit_letters in [d for d in digits if d not in mapping.values()]:
+            if mapping[4] < digit_letters:
+                mapping[9] = digit_letters
+
+        # Identify the 3, 1 is a subset of 3, which is a subset of 9
+        for digit_letters in [d for d in digits if d not in mapping.values()]:
+            if mapping[1] < digit_letters and digit_letters < mapping[9]:
+                mapping[3] = digit_letters
+
+        # Identify the 5, it is a strict subset of 9
+        for digit_letters in [d for d in digits if d not in mapping.values()]:
+            if mapping[9] > digit_letters:
+                mapping[5] = digit_letters
+
+        # Identify the 2, it's the last one with 5 segments
+        for digit_letters in [d for d in digits if d not in mapping.values()]:
+            if len(digit_letters) == 5:
+                mapping[2] = digit_letters
+
+        # Identify the 6, 5 is a strict subset of it
+        for digit_letters in [d for d in digits if d not in mapping.values()]:
+            if mapping[5] < digit_letters:
+                mapping[6] = digit_letters
+            else:
+                # Only 0 remains
+                mapping[0] = digit_letters
+
+        decoded_digits = []
+        for digit_to_decode in to_decode:
+            for k, v in mapping.items():
+                if v == digit_to_decode:
+                    decoded_digits.append(str(k))
+
+        decoded_digits = int("".join(decoded_digits))
+        print(decoded_digits)
+        s += decoded_digits
+
+print(s)