finished solution

master
Jeff Yates 2020-12-02 14:24:09 -05:00
parent 2d82a8d501
commit 6e3f8da8f5
1 changed files with 27 additions and 0 deletions

27
day02/solution2.rb Executable file
View File

@ -0,0 +1,27 @@
#!/bin/env ruby
def parse_line (line)
ret = Hash.new
ret[:pass] = line.split(':')[1].strip
ret[:char] = line.split(' ')[1][0]
ret[:min] = line.split(' ')[0].split('-')[0].to_i
ret[:max] = line.split(' ')[0].split('-')[1].to_i
return ret
end
results = {
:pass => 0,
:fail => 0
}
File.readlines('./input').each do |line|
password = parse_line(line)
c1 = password[:pass][password[:min] - 1]
c2 = password[:pass][password[:max] - 1]
if (c1 == password[:char]) ^ (c2 == password[:char])
results[:pass] += 1
else
results[:fail] += 1
end
end
pp results