From bcd8c8400eaaa8765d1cab9d8faa25d464706031 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Mon, 22 Nov 2021 14:01:31 +0100 Subject: [PATCH] Resolve day 4 --- 4/script.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 4/script.rb diff --git a/4/script.rb b/4/script.rb new file mode 100755 index 0000000..0953448 --- /dev/null +++ b/4/script.rb @@ -0,0 +1,24 @@ +#!/usr/bin/env ruby + +require 'digest' + +def part1(input) + find_md5 input +end + +def part2(input) + find_md5 input, 6 +end + +def find_md5(input, leading_zeros = 5) + (1..).detect do |i| + Digest::MD5.hexdigest(input + i.to_s).start_with?('0' * leading_zeros) + end +end + +input = 'yzbqklnj' + +puts "=== Part 1 ===" +puts part1(input) +puts "=== Part 2 ===" +puts part2(input)