summaryrefslogtreecommitdiffstats
path: root/20/py/d03.py
blob: 40b6b6c7748c0a573dc67fa0d7daed06fc8819bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import sys


def pt1(_in):
    squares = set()
    trees = set()
    for y in range(len(_in)):
        for x in range(len(_in[y])):
            if _in[y][x] == ".":
                squares.add((x, y))
            else:
                trees.add((x, y))
    h = len(_in)
    w = len(_in[0]) - 1 # \n
    amount = 0
    x, y = 0, 0
    while y < h:
        x += 3
        y += 1
        if (x % w, y) in trees:
            amount += 1
    return amount


def pt2(_in):
    squares = set()
    trees = set()
    for y in range(len(_in)):
        for x in range(len(_in[y])):
            if _in[y][x] == ".":
                squares.add((x, y))
            else:
                trees.add((x, y))
    h = len(_in)
    w = len(_in[0]) - 1 # \n
    amounts = []
    for slope in [(1,1), (3,1), (5,1), (7,1), (1,2)]:
        amount = 0
        x, y = 0, 0
        while y < h:
            x += slope[0]
            y += slope[1]
            if (x % w, y) in trees:
                amount += 1
        amounts.append(amount)
    s = 1
    for a in amounts:
        s *= a
    return s


if __name__ == "__main__":
    input = open("../input/03", "r").readlines()
    #input = [line for line in sys.stdin]
    print(pt1(input))
    print(pt2(input))