feat: add code for day 4

This commit is contained in:
Guillaume Dott 2019-12-21 12:07:43 +01:00
parent f0138f708a
commit 0fd1eecefa

28
4/script.rb Executable file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env ruby
require_relative '../common'
RANGE = '372037-905157'
def part1
first, last = RANGE.split('-')
(first..last).count do |password|
password = password.to_s
password.chars == password.chars.sort &&
password.match?(/(.)\1/)
end
end
def part2
first, last = RANGE.split('-')
(first..last).count do |password|
password = password.to_s
password.chars == password.chars.sort &&
password.chars.group_by(&:itself).any? { |_,v| v.size == 2 }
end
end
puts "=== Part 1 ==="
puts part1
puts "=== Part 2 ==="
puts part2