DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 1: Report Repair

Collapse
 
karaajc profile image
Kara Carrell

Hey Y'all!!! I thought I'd actually try making this happen this year and I completed the first challenges today!! I think in later challenges I'll actually try importing the text file as it is, but I wanted to make it fun for myself. I did the solution this time in ruby, and its a program you'd run on irb, that asks for the expenses and the factor you need to group to find the magic number (2 or 3) and it prints out the number to enter at the end.

Here's the way I solved it:

class AdventAccount

  def initialize(expenses, factor=2)
    @expenses = expenses
    @factor = factor
  end

  def perform
    filter_big_nums
    winning_number(do_si_do)
  end

  private

  def filter_big_nums
    @expenses.reject! { |expense| expense >= 2020 }
  end

  def find_your_partner
    @expenses.combination(@factor).to_a()
  end

  def winning_set(group)
    group.sum == 2020
  end

  def do_si_do
    find_your_partner.each do |group|
      puts "Checking group #{group}"
      return group if winning_set(group)
    end
  end

  def winning_number(group)
    puts "the winning number is #{group.reduce(:*)}"
  end

end

puts "Starting up the Advent Account checker"
puts "Submit expenses now:"
expenses = gets.chomp.split(",").map(&:to_i)
puts "Thanks! Now, what factor should we search the expenses with?"
factor = gets.chomp.to_i
test = AdventAccount.new(expenses, factor)

test.perform
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rpalo profile image
Ryan Palo

Ooh filtering out numbers > 2020 is a really nice touch. I hadn’t thought of that 😁