41 lines
831 B
Ruby
41 lines
831 B
Ruby
#!/bin/env ruby
|
|
|
|
#part 1
|
|
ids = File.readlines('./input').map(&:strip)
|
|
checksum = {twos: 0, threes: 0}
|
|
|
|
chars = ids.map { |id| id.each_char.tally.delete_if { |char, count| count==1 }}
|
|
chars.each do |id|
|
|
checksum[:twos] += 1 if id.has_value?(2)
|
|
checksum[:threes] += 1 if id.has_value?(3)
|
|
end
|
|
|
|
puts checksum[:twos] * checksum[:threes]
|
|
|
|
#part 2
|
|
stop = false
|
|
count = 0
|
|
match = []
|
|
ids.each do |id|
|
|
ids.each do |id2|
|
|
next if id == id2
|
|
count = 0
|
|
id.each_char.with_index do |char, i|
|
|
count += 1 if char != id2[i]
|
|
break if count > 1
|
|
end
|
|
if count == 1
|
|
puts id2
|
|
match.push(id2)
|
|
break
|
|
end
|
|
end
|
|
if count ==1
|
|
puts id
|
|
match.push(id)
|
|
break
|
|
end
|
|
end
|
|
|
|
puts (match[0].chars & match[1].chars).join
|