You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
937 B
41 lines
937 B
|
|
|
|
# count trees when going 1 down and 2 right
|
|
def tree_count(ls):
|
|
x = 0
|
|
trees = 0
|
|
for y, line in enumerate(ls):
|
|
if line[x] == '#':
|
|
trees += 1
|
|
x = (x + 3) % (len(line) - 1)
|
|
return trees
|
|
|
|
|
|
# count trees when going dx right and dy down
|
|
def tree_count2(ls, dx, dy):
|
|
x = 0
|
|
y = 0
|
|
trees = 0
|
|
while True:
|
|
try:
|
|
if ls[y][x] == '#':
|
|
trees += 1
|
|
x = (x + dx) % (len(ls[y]) - 1)
|
|
y = y + dy
|
|
except IndexError:
|
|
break
|
|
return trees
|
|
|
|
|
|
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input03") as f:
|
|
lines = f.readlines()
|
|
|
|
# Count trees on way down
|
|
print(tree_count(lines))
|
|
|
|
# Multiply trees on different slopes
|
|
print(tree_count2(lines, 1, 1) *
|
|
tree_count2(lines, 3, 1) *
|
|
tree_count2(lines, 5, 1) *
|
|
tree_count2(lines, 7, 1) *
|
|
tree_count2(lines, 1, 2))
|
|
|