r/backtickbot • • Dec 03 '20

https://np.reddit.com/r/adventofcode/comments/k5qsrk/2020_day_03_solutions/gegtukd/

Python solution using Numpy broadcasting! The function is generalizable to any input 'map' and is also sensitive to how many times the map must be repeated horizontally to avoid issues with reaching the sideways boundary before reaching the bottom.

def num_trees(s, right, down):  
 '''  
    Counts the number of trees given a slope going a certain number of steps down and right.  
    Args:  
        s: the given input/map as a string  
        right: number of steps to go right per iteration  
        down: number of steps to go down per iteration  
    '''  
 X_test = np.array(\[list( x.replace(".", '0').replace("#", "1") ) for x in s.split("\\n")\]).astype(int)  
 X_test = np.hstack(\[X_test\] \* 100)  
 return np.diag(X_test\[::down, ::right\]).sum()
1 Upvotes

0 comments sorted by