From 506621790a1f7cbbad37048b6490cb5838d51869 Mon Sep 17 00:00:00 2001 From: Jeff Yates Date: Thu, 3 Dec 2020 10:39:34 -0500 Subject: [PATCH] finished solution --- day03/solution2.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 day03/solution2.rb diff --git a/day03/solution2.rb b/day03/solution2.rb new file mode 100755 index 0000000..7334812 --- /dev/null +++ b/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(:*)}"