2020/3/count-trees-on-route-parameterised.rb
# Function for calculating trees encountered on specified slope
Function for calculating trees encountered on specified slope
Function for calculating trees encountered on specified slope
def count_trees_hit(map, dx, dy)
    number_of_trees_hit = 0
    x = dx
    width = map[0].length - 1
    map[dy..-1].each_slice(dy).map(&:first).each do |line|
        if line[x % width] == "#" then
            number_of_trees_hit += 1
        end
        x += dx
    end
    return number_of_trees_hit
end
def count_trees_hit ( map , dx , dy ) number_of_trees_hit = 0 x = dx width = map [ 0 ] . length - 1 map [ dy .. - 1 ] . each_slice ( dy ) . map ( & :first ) . each do | line | if line [ x % width ] == "#" then number_of_trees_hit += 1 end x += dx end return number_of_trees_hit end
Function for calculating trees encountered on specified slope
Function for calculating trees encountered on specified slope
# puts "width: #{width}" # puts "x: #{x}, x%width: #{x%width}, symbol: #{line[x % width]}"
map
<not_specific>
dx
<not_specific>
dy
<not_specific>