summary refs log tree commit diff
path: root/22.22
diff options
context:
space:
mode:
authordzwdz2022-12-22 21:50:50 +0100
committerdzwdz2022-12-22 21:50:50 +0100
commite4c6caa96475ad0e68a3a46cd4c2721159ab0dcf (patch)
tree56a90a0a1048ba97022490ea051e43139aea9908 /22.22
parent7f94cc101042416867870dcbcf99b6c5a87dd4e9 (diff)
minor cleanup
Diffstat (limited to '22.22')
-rw-r--r--22.22/part2.py26
1 files changed, 13 insertions, 13 deletions
diff --git a/22.22/part2.py b/22.22/part2.py
index d16635f..378f448 100644
--- a/22.22/part2.py
+++ b/22.22/part2.py
@@ -9,8 +9,6 @@ data = [list(line.strip('\n')) for line in data[:-2]]
 area = len([c for c in sum(data, start=[]) if not c.isspace()])
 sideLen = int(sqrt(area / 6))
 
-tiny = [a[::sideLen] for a in data[::sideLen]]
-
 def dot(a, b):
     assert len(a) == len(b)
     return sum([i*j for i,j in zip(a, b)])
@@ -18,12 +16,17 @@ def dot(a, b):
 def matmul(a, b):
     # matrix storage is flipped
     # that is - [[1,2,3]] is a column vector
-    assert len(a) == len(b[0])
-    m = [[0] * len(a[0]) for _ in b]
-    for i in range(len(m[0])):
-        for j in range(len(m)):
-            m[j][i] = dot(b[j], [col[i] for col in a])
-    return m
+    if type(b) == list:
+        # matrix * matrix
+        assert len(a) == len(b[0])
+        m = [[0] * len(a[0]) for _ in b]
+        for i in range(len(m[0])):
+            for j in range(len(m)):
+                m[j][i] = dot(b[j], [col[i] for col in a])
+        return m
+    else:
+        # matrix * scalar
+        return [[n * b for n in col] for col in a]
 
 def matadd(a, b):
     assert len(a) == len(b)
@@ -34,9 +37,6 @@ def matadd(a, b):
             m[j][i] = a[j][i] + b[j][i]
     return m
 
-def matmulsc(a, b):
-    return [[n * b for n in col] for col in a]
-
 def only(a):
     assert len(a) == 1
     return a[0]
@@ -80,7 +80,7 @@ def step(wall, f):
     def edge(f):
         v1 = wall[f%4]
         v2 = wall[(f+1)%4]
-        return matmulsc(matadd(v1, v2), 0.5)
+        return matmul(matadd(v1, v2), 0.5)
     shared = edge(f) # the shared edge
     prev = edge(f+2) # the edge opposite to shared; will become shared after rot
 
@@ -119,7 +119,7 @@ def cubestep(x, y, f):
     def edge(wall, f):
         v1 = wall[f%4]
         v2 = wall[(f+1)%4]
-        return matmulsc(matadd(v1, v2), 0.5)
+        return matmul(matadd(v1, v2), 0.5)
 
     shared = edge(bwall, f)
     visited = set()