finished solution

master
Jeff Yates 2 years ago
parent 17ec01ad9e
commit 506621790a
  1. 38
      day03/solution2.rb

@ -0,0 +1,38 @@
#!/bin/env ruby
slopes = {
:one => { :right => 1, :down => 1},
:two => { :right => 3, :down => 1},
:three => { :right => 5, :down => 1},
:four => { :right => 7, :down => 1},
:five => { :right => 1, :down => 2}
}
input = Array.new
File.readlines('./input').each.with_index do |line, i|
line.strip!
input[i] = line.split('')
end
input.each do |x|
x.map! do |y|
y == '#' ? true : false
end
end
collisions = Array.new
slopes.each do |num, slope|
i = 0
trees = 0
len = input[0].length
input.each.with_index do |row, row_num|
next if (slope[:down] == 2 and row_num.odd?)
trees += 1 if row[i]
i +=slope[:right]
i = i - len if i >= len
end
collisions.push(trees)
puts "On slope #{num.to_s} We hit #{trees} trees"
end
puts "Multiplied together we get: #{collisions.inject(:*)}"
Loading…
Cancel
Save