A bit late to the party, here are my solutions on Ruby:
Part 1:
twos = 0 threes = 0 File.open('day-02_input.txt').each_line do |line| counter = {} line.each_char do |char| next if line.count(char) <= 1 counter[char] = line.count(char) end twos += 1 if counter.value? 2 threes += 1 if counter.value? 3 end puts twos * threes
Part 2:
boxes = [] File.open('day-02_input.txt').each_line { |box| boxes << box.chomp! } def find_distance pair, distance = 0 pair[0].each_char.with_index do |char, index| distance += 1 if char != pair[1][index] end distance end boxes.combination(2).each do |pair| next unless find_distance(pair) == 1 str = '' pair[0].chars.each_with_index do |_, i| str << pair[0][i] if pair[0][i] == pair[1][i] end puts str end
I learnt about the combination method :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A bit late to the party, here are my solutions on Ruby:
Part 1:
Part 2:
I learnt about the combination method :)