diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-12-03 06:13:44 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-12-03 06:13:44 +0100 |
| commit | 383825b1b6201dc90c350abbc7ae5a28031f7e96 (patch) | |
| tree | acca883353a9b2ded281c76375ea7b273aec2a98 | |
| parent | b4cefdca7c5269b49ece02fb023bd451ae0f906d (diff) | |
| download | aoc-383825b1b6201dc90c350abbc7ae5a28031f7e96.tar.gz | |
day 03
| -rw-r--r-- | 20/py/d03.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/20/py/d03.py b/20/py/d03.py new file mode 100644 index 0000000..40b6b6c --- /dev/null +++ b/20/py/d03.py @@ -0,0 +1,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)) |
