Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
579 B · 22 lines
Ruby
at rust
1234567891011121314151617181920212223#!/usr/bin/env ruby# frozen_string_literal: true
ids = File.readlines('input.txt')
twos = 0threes = 0
ids.each do |id| counts = id.chars.each_with_object(Hash.new(0)) { |e, h| h[e] += 1 } twos += 1 if counts.any? { |_k, v| v == 2 } threes += 1 if counts.any? { |_k, v| v == 3 }end
puts "Part 1: #{twos * threes}"
def check(a, b) diff_indices = a.each_char.with_index.reject { |c, i| c == b[i] }.map(&:last) $diff_i = diff_indices.first if diff_indices.size == 1end
puts "Part 2: #{ids.combination(2).find { |c| check *c }.first.tap { |s| s.slice! $diff_i }}"