From 73a212ce1efcaf389605f43bbf58833aa7b6c7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 16 Dec 2019 15:17:47 +0100 Subject: Day 16 py Runtime is shit, about 20-30 seconds on a ryzen 2600 --- solutions/input/16 | 1 + solutions/py/d16.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 solutions/input/16 create mode 100644 solutions/py/d16.py diff --git a/solutions/input/16 b/solutions/input/16 new file mode 100644 index 0000000..a424590 --- /dev/null +++ b/solutions/input/16 @@ -0,0 +1 @@ +59766832516471105169175836985633322599038555617788874561522148661927081324685821180654682056538815716097295567894852186929107230155154324411726945819817338647442140954601202408433492208282774032110720183977662097053534778395687521636381457489415906710702497357756337246719713103659349031567298436163261681422438462663511427616685223080744010014937551976673341714897682634253850270219462445161703240957568807600494579282412972591613629025720312652350445062631757413159623885481128914333982571503540357043736821931054029305931122179293220911720263006705242490442826574028623201238659548887822088996956559517179003476743001815465428992906356931239533104 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)) -- cgit v1.2.1