# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="Vc7TGwZa5eQX"
import numpy as np

# + colab={"base_uri": "https://localhost:8080/"} id="QxUPfFdL5xKU" outputId="6f8788e0-27f7-4223-bbd0-5ee9096c71ae"
data_path = "/content/drive/MyDrive/adventofcode/2020/data_3.txt"
with open(data_path) as f:
    tree_map = np.array([list(word) for word in f.read().splitlines()])
print(tree_map.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="GUClTmQcE8DS" outputId="e2b591c7-ab07-4bfe-91e9-def7d44a79e7"
slopes = [(1,1),(3,1),(5,1),(7,1),(1,2)]

def count_trees(tree_map, slopes):
    trees = []
    for slope in slopes:
        tree_counter = 0
        v_step, h_step = slope
        v_index = v_step
        for i in range(h_step,len(tree_map), h_step):
            v_index = v_index % len(tree_map[i]) #modulo index if out of bounds
            if tree_map[i,v_index] == "#":
                tree_counter +=1
            v_index += v_step
        trees.append(tree_counter)
    return np.array(trees)

result = count_trees(tree_map, slopes)
print("#trees encountered per slope: ", result)
print("Answer part 1 -- #trees encountered for slope(3,1): ", result[1])
print("Answer part 2 -- #trees encountered per slope multiplied: ", result.prod())

StarcoderdataJupyterScriptsDedupFiltered