summary refs log blame commit diff stats
path: root/LICENSE
blob: a5893df774e901efb834f9050ed4600a7ebe2d21 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13












                                                                        
Copyright (c) 2020, Andinus <andinus@inventati.org>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
old } /* Literal.Number.Integer.Long */
#!/usr/bin/env python

import numpy as np
from scipy.ndimage import label

with open("day9.txt") as data:
    depthmap = []
    for line in data:
        row = [int(num) for num in line.strip()]
        depthmap.append(row)
    depthmap = np.array(depthmap)

# part 1
# convolve across grid

def conv_filter(position: (int, int)) -> int:
    def neighbors():
        x, y = position
        neighboring = {(x-1, y), (x+1, y), (x, y-1), (x, y+1)}
        return {(xPos, yPos) for xPos, yPos in neighboring if 0 <= xPos <= depthmap.shape[0]-1 and 0 <= yPos <= depthmap.shape[1]-1}

    if all(depthmap[position] < depthmap[pos] for pos in neighbors()):
        return depthmap[position] + 1
    else:
        return 0

print(sum(conv_filter((x, y)) for x in range(depthmap.shape[0]) for y in range(depthmap.shape[1])))

# part 2
basins, num_basins = label(depthmap != 9)
counts = [np.count_nonzero(basins == num) for num in range(1, num_basins + 1)]
counts.sort()
print(np.prod(counts[-3:]))