about summary refs log tree commit diff
path: root/users/Profpatsch/advent-of-code/2020/03/main.py
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2021-02-27T20·46+0100
committerProfpatsch <mail@profpatsch.de>2021-02-27T20·48+0000
commitace8c656befb37ddb558836d4787a775331c78f3 (patch)
tree0d272ddfd9958bb236449e0168cff5d891c82304 /users/Profpatsch/advent-of-code/2020/03/main.py
parentf3d00b84bb65ae344a28e1bebfb5ce8b0efff8d1 (diff)
feat(users/Profpatsch): add advent of code 2020 day 1 2 3 r/2245
Change-Id: I99d2882ac9ef5ede85032132f6727e7bad8f24eb
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2564
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch/advent-of-code/2020/03/main.py')
-rw-r--r--users/Profpatsch/advent-of-code/2020/03/main.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/users/Profpatsch/advent-of-code/2020/03/main.py b/users/Profpatsch/advent-of-code/2020/03/main.py
new file mode 100644
index 000000000000..4d6baf946c3e
--- /dev/null
+++ b/users/Profpatsch/advent-of-code/2020/03/main.py
@@ -0,0 +1,66 @@
+import itertools
+import math
+
+def tree_line(init):
+    return {
+        "init-len": len(init),
+        "known": '',
+        "rest": itertools.repeat(init)
+    }
+
+def tree_line_at(pos, tree_line):
+    needed = (pos + 1) - len(tree_line["known"])
+    # internally advance the tree line to the position requested
+    if needed > 0:
+        tree_line["known"] = tree_line["known"] \
+          + ''.join(
+            itertools.islice(
+                tree_line["rest"],
+                1+math.floor(needed / tree_line["init-len"])))
+    # print(tree_line)
+    return tree_line["known"][pos] == '#'
+
+def tree_at(linepos, pos, trees):
+    return tree_line_at(pos, trees[linepos])
+
+def slope_positions(trees, right, down):
+    line = 0
+    pos = 0
+    while line < len(trees):
+        yield (line, pos)
+        line = line + down
+        pos = pos + right
+
+trees = []
+with open("./input", 'r') as f:
+    for line in f:
+        line = line.rstrip()
+        trees.append(tree_line(line))
+
+# print(list(itertools.islice(trees[0], 5)))
+# print(list(map(
+#     lambda x: tree_at(0, x, trees),
+#     range(100)
+# )))
+# print(list(slope_positions(trees, right=3, down=1)))
+
+def count_slope_positions(trees, slope):
+    count = 0
+    for (line, pos) in slope:
+        if tree_at(line, pos, trees):
+            count = count + 1
+    return count
+
+print(
+        count_slope_positions(trees, slope_positions(trees, right=1, down=1))
+    *
+        count_slope_positions(trees, slope_positions(trees, right=3, down=1))
+    *
+        count_slope_positions(trees, slope_positions(trees, right=5, down=1))
+    *
+        count_slope_positions(trees, slope_positions(trees, right=7, down=1))
+    *
+        count_slope_positions(trees, slope_positions(trees, right=1, down=2))
+)
+
+# I realized I could have just used a modulo instead …