-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.rb
30 lines (25 loc) · 823 Bytes
/
day02.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
class PwdEntry
def initialize(letter, n1, n2, password)
@letter = letter
@n1 = n1
@n2 = n2
@password = password
end
def policy1_valid
count = @password.chars.count{|c| c == @letter}
return count >= @n1 && count <= @n2
end
def policy2_valid
chars = @password.chars
return chars[@n1 - 1] == @letter && chars[@n2 - 1] != @letter || chars[@n1 - 1] != @letter && chars[@n2 - 1] == @letter
end
end
def parsePwdEntry(line)
rx = /([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)/
md = rx.match(line)
return PwdEntry.new(md[3], md[1].to_i, md[2].to_i, md[4])
end
file = File.open("input/day02.txt", "r")
pwds = file.readlines.map {|line| parsePwdEntry(line) }
puts pwds.count { |e| e.policy1_valid }
puts pwds.count { |e| e.policy2_valid }