From fc0c4a3195627d0db77bf5701eeebf8706de8b77 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Sat, 4 Dec 2021 21:34:55 +0000 Subject: day 4: numpy version --- 4np.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 4np.py diff --git a/4np.py b/4np.py new file mode 100644 index 0000000..ad04746 --- /dev/null +++ b/4np.py @@ -0,0 +1,17 @@ +import numpy as np +from utils import open_day + +BOARD_SIZE = 5 + +nums, boards = open_day(4).read().rstrip().split('\n\n', maxsplit=1) +nums = np.fromstring(nums, sep=',', dtype=int) +numidxs = np.empty(nums.size, dtype=int) +numidxs[nums] = np.arange(nums.size) +boards = np.fromstring(boards.replace('\n', ' '), sep=' ', dtype=int).reshape(-1, BOARD_SIZE, BOARD_SIZE) +rboards = numidxs[boards] +colmins = np.amin(np.amax(rboards, axis=1), axis=1) +rowmins = np.amin(np.amax(rboards, axis=2), axis=1) +mins = np.amin(np.stack((colmins, rowmins)), axis=0) +sums = np.sum(boards, axis=(1, 2), where=(rboards > np.expand_dims(mins, (-1, -2)))) * nums[mins] +print(sums[np.argmin(mins)]) +print(sums[np.argmax(mins)]) -- cgit v1.2.3-54-g00ecf