diff --git a/4/script.rb b/4/script.rb new file mode 100755 index 0000000..bce660e --- /dev/null +++ b/4/script.rb @@ -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