DEV Community

Discussion on: Advent of Code 2020 Solution Megathread - Day 6: Custom Customs

Collapse
 
sleeplessbyte profile image
Derk-Jan Karrenbeld

Only took a few minutes today; here is what I got in Ruby:

require 'benchmark'

class GroupAnswers
  def self.from_lines(lines)
    GroupAnswers.new(lines.map(&:chomp))
  end

  def initialize(people)
    questions = people.join('').chars.uniq
    self.count = questions.count { |q| people.all? { |l| l.include?(q) } }
  end

  def to_i
    self.count
  end

  private

  attr_accessor :count
end

groups = File
  .read('input.txt')
  .split(/\n\n/)

Benchmark.bmbm do |b|
  b.report do
    puts groups.sum { |group| GroupAnswers.from_lines(group.split(/\n/)).to_i }
  end
end
Enter fullscreen mode Exit fullscreen mode