From e05d0c55787fa42635982090ef76e3a168d53ac8 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Tue, 17 Dec 2019 17:47:13 -0500 Subject: [PATCH] Speed up day 16 --- day16/py/main.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/day16/py/main.py b/day16/py/main.py index aefd05c..c9cc25e 100644 --- a/day16/py/main.py +++ b/day16/py/main.py @@ -1,6 +1,6 @@ import sys import math -from typing import Tuple +from typing import Tuple, List BASE_PATTERN = (0, 1, 0, -1) @@ -20,14 +20,17 @@ def stretch_pattern(i: int, length: int) -> Tuple[int, ...]: return trimmed_pattern -def generate_second_half(input_num: str): - res = '' +def generate_second_half(input_num: str) -> str: last_sum = 0 - for char in reversed(input_num): - last_sum = int(char) + int(last_sum) - res = str(last_sum % 10) + res + # It is much much faster if, instead of concating a result as a string, we replace the results in a list in-place + # We could probably make this faster if we mutated a list each time instead of making a new list, but this is + # good enough(tm) + input_list = [int(digit) for digit in input_num] + for i, digit in reversed(tuple(enumerate(input_list))): + last_sum = digit + last_sum + input_list[i] = str(last_sum % 10) - return res + return ''.join(input_list) def run_pattern_round(input_num: str, offset: int) -> str: @@ -62,7 +65,6 @@ def part1(input_num: str) -> str: def part2(input_num: str) -> str: - # This is super super slow (about 7 mins on my PC.) Need to find a way to speed it up offset = int(input_num[:7]) return generate_all_pattern_rounds(input_num * 10000, offset)[:8]