master
Raw Download raw file
 1import numpy
 2
 3
 4def read_input():
 5    with open("input/d03p1.txt") as f:
 6        return f.readlines()
 7
 8
 9def parse(line):
10    line = line.strip()
11    tree_list = []
12    for c in line:
13        if c == ".":
14            tree_list.append(0)
15        if c == "#":
16            tree_list.append(1)
17
18    return tree_list
19
20
21def aboreal_count(x_rate, y_rate, tree_field):
22    count = 0
23    for i in range(0, len(tree_field)):
24        y = i * y_rate
25        x = (i * x_rate) % len(tree_field[0])
26        if y > len(tree_field):
27            break
28        count += tree_field[y][x]
29    return count
30
31
32input_lines = read_input()
33parsed_lines = [parse(line) for line in input_lines]
34print("p1:", aboreal_count(3, 1, parsed_lines))
35p2_rates = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
36p2_counts = [aboreal_count(d[0], d[1], parsed_lines) for d in p2_rates]
37print("p2:", numpy.prod(p2_counts))