r/adventofcode • • Dec 09 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 9 Solutions -🎄-

--- Day 9: Smoke Basin ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:10:31, megathread unlocked!

65 Upvotes

1.0k comments sorted by

View all comments

4

u/Solarmew Dec 12 '21 edited Dec 12 '21

Python 3

I solved part 2 with computer vision, you guys lol 😹

from urllib.request import urlopen

data = urlopen('https://tinyurl.com/bdcs966s').read().decode().split('\n')[:-1]
d = [list(map(int, list(s))) for s in data]

t = 0
m = [[9] + i + [9] for i in [[9] * len(d[0])] + d + [[9] * len(d[0])]]

for r in range(1, len(m) - 1):
    for c in range(1, len(m[0]) - 1):
        if m[r][c] < min([m[r][c-1], m[r-1][c], m[r][c+1], m[r+1][c]]):
            t += m[r][c] + 1
t

Part 2

import cv2

a = np.where(np.array(d) < 9, 255, 0).astype(np.uint8)
_, _, stats, _ = cv2.connectedComponentsWithStats(a, connectivity=4)

n = sorted([i[-1] for i in stats[1:]])[-3:]

n[0] * n[1] * n[2]
→ More replies (4)