Advent-of-Code/2020/Day 3/Solution.py

42 lines
1 KiB
Python
Raw Permalink Normal View History

input_file = "input.txt"
2020-12-04 23:10:35 +01:00
map_table = []
with open(input_file) as slope_map:
line = slope_map.readline()
while line and line != "\n":
map_table.append(line)
line = slope_map.readline()
def check_for_trees(direction=(3, 1)):
2020-12-04 23:10:35 +01:00
x_pos = 0
y_pos = 0
width = len(map_table[0])
height = len(map_table)
tree_count = 0
# Might pose an issue for y speed different than a divisor of the number of lines ?
2020-12-04 23:10:35 +01:00
while y_pos < height-1:
# x wraps around as the map is repeated on the right indefinitely
x_pos, y_pos = ((x_pos+direction[0]) % (width-1), y_pos+direction[1])
2020-12-04 23:10:35 +01:00
if map_table[y_pos][x_pos] == "#":
tree_count += 1
print(f"We came close to {tree_count} trees by going {direction}.")
return tree_count
2020-12-04 23:10:35 +01:00
def check_multiple_directions(directions):
result = 1
for direction in directions:
result *= check_for_trees(direction)
print(f"Result is {result}.")
check_multiple_directions([(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)])