finished solution

master
Jeff Yates 2020-12-03 10:39:34 -05:00
parent 17ec01ad9e
commit 506621790a
1 changed files with 38 additions and 0 deletions

38
day03/solution2.rb Executable file
View File

@ -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(:*)}"