From 7cd1acb39af07d052f280fb23c82fe40a0ce8494 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Tue, 21 Dec 2021 13:42:01 +1030 Subject: [PATCH] Minor dtype optimisation --- 2021/day20.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/2021/day20.py b/2021/day20.py index aeec33a..f976746 100644 --- a/2021/day20.py +++ b/2021/day20.py @@ -1,4 +1,7 @@ import numpy as np +from sys import argv + +filename = argv[1] if len(argv)>1 else 'day20input' def unfold(func, arg, n): for _ in range(n): @@ -7,15 +10,16 @@ def unfold(func, arg, n): mapping = {'#':1, '.':0} -with open('day20input', 'r') as file: +with open(filename, 'r') as file: rawinput = file.read().strip() cipher_str, _, input_str = rawinput.partition('\n\n') - cipher = np.array([mapping[c] for c in cipher_str]) - input_arr = np.array([[mapping[c] for c in string] for string in input_str.split('\n')]) + cipher = np.array([mapping[c] for c in cipher_str], dtype=np.int8) + input_arr = np.array([[mapping[c] for c in string] for string in input_str.split('\n')], dtype=np.int8) roll_offsets = [(x,y) for y in [1,0,-1] for x in [1,0,-1]] # Note roll is opposite direction to our sampling i.e. (1,1) means (0,0) is old (-1,-1) def sample(array): - lookup_values = sum([2**(8-n)*np.roll(array, offset, (1,0)) for n,offset in enumerate(roll_offsets)]) # Note axis 1 is x axis 0 is y. + #lookup_values = sum([2**(8-n)*np.roll(array, offset, (1,0)) for n,offset in enumerate(roll_offsets)]) # Note axis 1 is x axis 0 is y. + lookup_values = np.sum([2**(8-n)*np.roll(array, offset, (1,0)) for n,offset in enumerate(roll_offsets)], 0, dtype=np.int16) # Note axis 1 is x axis 0 is y. return cipher[lookup_values] def visualize(array):