adventofcode/2020/3/solve.py

45 lines
973 B
Python
Raw Normal View History

2022-12-01 21:13:55 +01:00
#!/usr/bin/env python3
class cursor():
def __init__(self, game_map):
self.x, self.y = 0, 0
self.limit_x = len(game_map[1])
self.map = game_map
def setx(self, x):
self.x = x % self.limit_x
def sety(self, y):
self.y = y
def isTree(self):
line = self.map[y]
char = line[self.x]
return char == '#'
with open('input', 'r') as f:
game_map = list(map(str.strip, f.readlines()))
slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
cur = cursor(game_map)
tree_list = []
for slope in slopes:
print(slope)
x, y = 0, 0
cur.setx(x)
cur.sety(y)
try:
trees = 0
while True:
x += slope[0]
y += slope[1]
cur.setx(x)
cur.sety(y)
if cur.isTree():
trees += 1
except IndexError:
tree_list.append(trees)
result = 1
print(tree_list)
for res in tree_list:
result *= res
print(result)