aoc/2020/03/03.py

38 lines
839 B
Python
Raw Normal View History

2020-12-03 20:41:28 +00:00
def read_file():
with open("input.txt","r") as f:
return f.read().split('\n')
def part1(tree_map):
width = len(tree_map[0])
trees_encountered = 0
for i, line in enumerate(tree_map):
if tree_map[i][3*i % width] == "#":
trees_encountered += 1
return trees_encountered
def part2(tree_map, right, down):
height = len(tree_map)
width = len(tree_map[0])
trees_encountered = 0
i = 0
j = 0
while i < height:
if tree_map[i][(j % width)] == "#":
trees_encountered += 1
i += down
j += right
return trees_encountered
def main():
tree_map = read_file()
print(f'Part 1: {part1(tree_map)}')
a = part2(tree_map, 1, 1)
b = part2(tree_map, 3, 1)
assert b == part1(tree_map)
c = part2(tree_map, 5, 1)
d = part2(tree_map, 7, 1)
e = part2(tree_map, 1, 2)
print(f'Part 2: {a*b*c*d*e}')
if __name__ == "__main__":
main()