2020-12-05 00:04:09 +01:00
|
|
|
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()
|
|
|
|
|
2020-12-05 00:04:09 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-12-05 00:04:09 +01:00
|
|
|
# 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:
|
2020-12-05 00:04:09 +01:00
|
|
|
# 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-05 00:04:09 +01:00
|
|
|
|
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}.")
|
|
|
|
|
2020-12-05 00:04:09 +01:00
|
|
|
|
|
|
|
check_multiple_directions([(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)])
|