summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-04 21:34:55 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-04 21:34:55 +0000
commitfc0c4a3195627d0db77bf5701eeebf8706de8b77 (patch)
tree090236702c4e00416c04d79066d5acd5925c32dd
parent7a15d394d4cbac2827ce621e58f9c267f3a20df8 (diff)
downloadaoc2021-fc0c4a3195627d0db77bf5701eeebf8706de8b77.tar.gz
aoc2021-fc0c4a3195627d0db77bf5701eeebf8706de8b77.tar.xz
aoc2021-fc0c4a3195627d0db77bf5701eeebf8706de8b77.zip
day 4: numpy version
-rw-r--r--4np.py17
1 files changed, 17 insertions, 0 deletions
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)])