Advent of Code 2020: Day 6 (Ruby solution)

Subscribe to my newsletter and never miss my upcoming articles

This time input file contains many groups of entries. Entries are separated with a single newline character while groups are separated with two of them.

Your job in the first puzzle is to find how many unique letters occur in each group and count them together.

In the second task, you are supposed to check how many unique letters occur across all entries of each group and to count them together.

So basically, solutions of those 2 tasks differ in one operator. In the first task, it is a union (| operator) and in the second one, it is an intersection (& operator).

def count_letters file, operator
  letters_count = 0
  file.each_line("\n\n") do |group|
    letters_count += { |line| line.strip.chars }.inject(operator).size

file ='inputs/day6.txt')
puts "First task answer: #{count_letters(file, :|)}"
puts "Second task answer: #{count_letters(file, :&)}"

No Comments Yet