summaryrefslogtreecommitdiffstats
path: root/solutions/py/d16.py
diff options
context:
space:
mode:
authorGustav Sörnäs <gusso230@student.liu.se>2019-12-16 15:17:47 +0100
committerGustav Sörnäs <gusso230@student.liu.se>2019-12-16 15:17:47 +0100
commit73a212ce1efcaf389605f43bbf58833aa7b6c7b3 (patch)
treef30e548d4ddaa4b1dce0664e8038f7b49e415610 /solutions/py/d16.py
parent640bdc3fed46a9e6d3d569dae8fa91da2894697d (diff)
downloadaoc-73a212ce1efcaf389605f43bbf58833aa7b6c7b3.tar.gz
Day 16 py
Runtime is shit, about 20-30 seconds on a ryzen 2600
Diffstat (limited to 'solutions/py/d16.py')
-rw-r--r--solutions/py/d16.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/solutions/py/d16.py b/solutions/py/d16.py
new file mode 100644
index 0000000..01bb25e
--- /dev/null
+++ b/solutions/py/d16.py
@@ -0,0 +1,71 @@
+from profilehooks import coverage
+from profilehooks import profile
+
+#@coverage
+def do(input, phases=100, repeats=1):
+ nums = []
+ for i in range(repeats):
+ nums += [int(x) for x in input[0].strip()]
+
+ for phase in range(phases):
+ print(phase)
+ new_list = []
+ for i in range(len(nums)): # position of number to calculate
+ new_num = 0
+ for k in range(len(nums)):
+ mod = (k+1) % (4*(i+1))
+ if mod >= i + 1 and mod < 2*i + 2:
+ new_num += nums[k]
+ elif mod >= 3*i + 3:
+ new_num -= nums[k]
+ new_list.append(abs(new_num) % 10)
+ nums = new_list
+ return "".join([str(n) for n in nums[:8]])
+
+def pt1(input):
+ return do(input, phases=100, repeats=1)
+
+def pt2(input, phases=100, repeats=10000):
+ offset = int(input[0][:7])
+ nums = []
+ for i in range(repeats):
+ nums += [int(x) for x in input[0].strip()]
+ nums = nums[offset:]
+ for phase in range(phases):
+ print(phase)
+ nums = nums[::-1]
+ new_nums = []
+ n = sum(nums)
+ while len(nums) != 0:
+ new_nums.append(abs(n) % 10)
+ n -= nums.pop()
+ nums = new_nums
+ return "".join([str(n) for n in nums[:8]])
+
+def square(size):
+ s = ""
+ for y in range(size):
+ for x in range(size):
+ mod = (x+1) % (4*(y+1))
+ if mod < y+1:
+ s += "0"
+ elif mod < 2*y + 2:
+ s += "+"
+ elif mod < 3*y + 3:
+ s += "0"
+ else:
+ s += "-"
+ s += " "
+ s += "\n"
+ return s
+
+input = open("../input/16", "r").readlines()
+ans1 = pt1(input)
+ans2 = pt2(input)
+print(ans1)
+print(ans2)
+#print(do(["80871224585914546619083218645595"]))
+#print()
+#print(square(8))
+#print(square(20))
+#print(square(62))