summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gusso230@student.liu.se>2019-12-18 20:37:36 +0100
committerGustav Sörnäs <gusso230@student.liu.se>2019-12-18 20:37:36 +0100
commit0d473b23584e9f8e87856db167d285cd899347a4 (patch)
tree424a24142dfa604959e2047d6a2066e95c8cf407
parent824e73b75134bcf949f554d41986189a4dbded70 (diff)
downloadaoc-0d473b23584e9f8e87856db167d285cd899347a4.tar.gz
Implement LCM and remove coverage
Runtime is at 6 seconds with pypy oof.
-rw-r--r--19/py/d03.py3
-rw-r--r--19/py/d12.py17
-rw-r--r--19/py/main.py2
3 files changed, 6 insertions, 16 deletions
diff --git a/19/py/d03.py b/19/py/d03.py
index 70bf9f0..b1fa9d8 100644
--- a/19/py/d03.py
+++ b/19/py/d03.py
@@ -1,4 +1,3 @@
-from profilehooks import coverage
import math
def intersection(l1, l2):
@@ -9,7 +8,6 @@ def intersection(l1, l2):
def man_dist(point):
return abs(point[0]) + abs(point[1])
-#@coverage
def pt1(input):
wire1 = []
wire2 = []
@@ -41,7 +39,6 @@ def pt1(input):
dist = min(dist, man_dist(point))
return dist
-#@coverage
def pt2(input):
wire1 = {}
wire2 = {}
diff --git a/19/py/d12.py b/19/py/d12.py
index 878acc6..90ae437 100644
--- a/19/py/d12.py
+++ b/19/py/d12.py
@@ -1,5 +1,5 @@
import itertools
-#import primefac
+import math
class Moon(object):
def __init__(self, x, y, z):
@@ -47,8 +47,10 @@ class Moon(object):
def get(self):
return self.get_x, self.get_y, self.get_z
+def lcm(a, b):
+ return a*b // math.gcd(a, b)
+
def pt1(input):
- return
moons = []
for line in input:
@@ -93,7 +95,6 @@ def get_cycle(init_pos, init_vel):
return iters
def pt2(input):
- return
moons = []
xs = 0
ys = 0
@@ -110,15 +111,7 @@ def pt2(input):
ys = get_cycle([moon.y for moon in moons], [0 for _ in range(4)])
zs = get_cycle([moon.z for moon in moons], [0 for _ in range(4)])
- factors = [primefac.factorint(n) for n in (xs, ys, zs)]
- nums = {}
- for factor in factors:
- for n in factor:
- nums[n] = max(nums.get(n, 0), factor[n])
- ans = 1
- for n in nums:
- ans *= n ** nums[n]
- return ans
+ return lcm(lcm(xs, ys), zs)
if __name__ == "__main__":
input = open("../input/12", "r").readlines()
diff --git a/19/py/main.py b/19/py/main.py
index c45bac7..70cf792 100644
--- a/19/py/main.py
+++ b/19/py/main.py
@@ -21,7 +21,7 @@ import d17
mods = [d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, \
d11, d12, d13, d14, d15, d16, d17]
-skip = [12, 16]
+skip = []
timings = [[0 for _ in range(2)] for _ in range(len(mods))]
#clock_type = time.CLOCK_MONOTONIC