summaryrefslogtreecommitdiffstats
path: root/20/py
diff options
context:
space:
mode:
Diffstat (limited to '20/py')
-rw-r--r--20/py/d03.py57
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))