Advent of Code 2020: Day 3 (Ruby solution)

This time the input for puzzles is a text file that pretends to be a map. Your mission is to count the trees on your way.

In the first task, you move always 3 steps right and 1 step down. In the second task, there are 5 different ways of taking steps around the map. That what you are asked for is a mathematical product of numbers of trees on each way.

def trees_on_way file, step_down, step_left
  trees = 0
  file.each_line.with_index do |line, index|
    next unless index % step_down == 0
    position = ((index / step_down * step_left) % line.strip.size)
    trees += 1 if line[position] == '#'
  end
  trees
end

file = File.read('inputs/day3.txt')
puts trees_on_way(file, 1, 3)
puts [[1,1], [1, 3], [1, 5], [1, 7], [2, 1]]
         .map{|steps| trees_on_way(file, steps.first, steps.last)}.inject(:*)