-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.rb
executable file
·43 lines (37 loc) · 1.03 KB
/
day4.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env ruby
def has_double?(num)
num.to_s.match(/([0-9])\1/)
end
# Figure out if there's a double-digit that's
# not part of a larger group of digits.
# I'm sure there's a way to do this with a pure regex
# match or some kind of math, but... meh. This works.
# Replace any groups of three or more of a digit with _,
# then check for any remaining doubles.
def has_strict_double?(num)
has_double?(num.to_s.gsub(/([0-9])\1{2,}/, '_'))
end
def increasing?(num)
last = num.to_s[0]
num.to_s[1..-1].split('').each do |digit|
return false unless last <= digit
last = digit
end
true
end
def part1(from, to)
candidates = []
for candidate in from..to do
candidates << candidate if has_double?(candidate) && increasing?(candidate)
end
candidates.length
end
def part2(from,to)
candidates = []
for candidate in from..to do
candidates << candidate if increasing?(candidate) && has_strict_double?(candidate)
end
candidates.length
end
puts "Part 1: #{part1(273025,767253)}"
puts "Part 2: #{part2(273025,767253)}"