From 0fd1eecefa673bfd6a83aaf2119e9604f2e45376 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Sat, 21 Dec 2019 12:07:43 +0100 Subject: [PATCH] feat: add code for day 4 --- 4/script.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 4/script.rb 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