parent
536f505093
commit
bebcf0f46f
1 changed files with 29 additions and 2 deletions
@ -1,13 +1,40 @@ |
||||
#!/bin/env ruby |
||||
|
||||
#part 1 |
||||
ids = File.readlines('./input').map(&:strip) |
||||
checksum = {twos: 0, threes: 0} |
||||
|
||||
ids.map! { |id| id.each_char.tally.delete_if { |char, count| count==1 }} |
||||
ids.each do |id| |
||||
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 |
||||
|
Loading…
Reference in new issue