day3: add python solution

This commit is contained in:
Xiretza 2020-12-03 19:53:39 +01:00
parent 2e6907c604
commit 8503335031
Signed by: xiretza
GPG key ID: 17B78226F7139993
2 changed files with 21 additions and 0 deletions

View file

@ -8,3 +8,4 @@ https://adventofcode.com/2020/
|---|--------|---------|------|
| 1 | `**` | `**` | |
| 2 | `**` | | `**` |
| 3 | `**` | | |

20
day3/day3.py Executable file
View file

@ -0,0 +1,20 @@
#!/usr/bin/env python
def count_trees(right, down, lines):
n = 0
for i, line in enumerate(lines[::down]):
c = line[(i*right) % len(line)]
if c == '#':
n += 1
return n
if __name__ == '__main__':
with open('input.txt', 'r') as f:
lines = [line.strip() for line in f.readlines()]
print(count_trees(3, 1, lines))
hits_multiplied = 1
for slope in ((1,1), (3,1), (5,1), (7,1), (1,2)):
hits_multiplied *= count_trees(*slope, lines)
print(hits_multiplied)