Bläddra i källkod

Simplify day 24 with assumptions on input

Taddeus Kroes 4 år sedan
förälder
incheckning
8e5e4ca6ee
1 ändrade filer med 12 tillägg och 34 borttagningar
  1. 12 34
      2021/24_alu.py

+ 12 - 34
2021/24_alu.py

@@ -4,13 +4,6 @@ from operator import add, mul, floordiv, mod, eq
 
 OPS = {'add': add, 'mul': mul, 'div': floordiv, 'mod': mod, 'eql': eq}
 
-def isoffset(e):
-    return isinstance(e, Expr) \
-            and e.opcode == 'add' \
-            and isinstance(e.right, int) \
-            and isinstance(e.left, Expr) \
-            and e.left.opcode == 'inp'
-
 class Expr:
     def __init__(self, opcode, left, right):
         self.opcode = opcode
@@ -29,41 +22,26 @@ class Expr:
         if self.opcode == 'add':
             if self.right == 0:
                 return self.left
-            if rconst and self.left.opcode == 'add' and isinstance(self.left.right, int):
-                return Expr('add', self.left.left, self.left.right + self.right)
+            if self.left.opcode == 'add':
+                right = self.left.right + self.right
+                return Expr('add', self.left.left, right)
         elif self.opcode == 'mul':
             if self.right == 0:
                 return 0
             if self.right == 1:
                 return self.left
         elif self.opcode == 'eql':
-            if rconst:
-                if self.left.opcode == 'inp' and self.right < 1 or self.right > 9:
-                    return 0
-            elif self.right.opcode == 'inp':
-                if self.left.opcode == 'add' \
-                        and isinstance(self.left.right, int) \
-                        and self.left.right > 9:
-                    return 0
-                else:
-                    assert isoffset(self.left)
-                    index_a = self.left.left.left
-                    offset = self.left.right
-                    index_b = self.right.left
-                    assert index_a < index_b
-                    conditions.append((index_a, index_b, offset))
-                    return 1
+            offset = self.left.right
+            if rconst or offset > 9:
+                return 0
+            conditions.append((self.left.left.left, self.right.left, offset))
+            return 1
         elif self.opcode == 'div':
-            if self.right == 1:
-                return self.left
-            if rconst and self.left.opcode == 'add' \
-                    and isoffset(self.left.right) \
-                    and self.left.left.opcode == 'mul' \
-                    and self.left.left.right == self.right:
-                return self.left.left.left
+            return self.left if self.right == 1 else self.left.left.left
         elif self.opcode == 'mod':
-            if self.left.opcode == 'add' and self.right == 26:
-                return self.left if isoffset(self.left) else self.left.right
+            if self.left.left.opcode == 'inp':
+                return self.left
+            return self.left.right
 
         return self