r/backtickbot • u/backtickbot • Dec 02 '20
https://np.reddit.com/r/adventofcode/comments/k52psu/2020_day_02_solutions/gef04ld/
Done part 1 in Ruby and in Python
Ruby
"""
password-philosophy.rb: from day_2.txt find out how many passwords are valid according to their policies; solve puzzle at https://adventofcode.com/2020/day/2
2 December 2020
@Vicki_Langer
"""
valid_passwords = 0
passwords_with_policies = File.readlines("inputs/day_2.txt")
# \d numerical digit
# \w alphanumeric characters
# * zero or more
passwords_with_policies. each do |line|
low, high, character, password = line.match(/(\d*)-(\d*) (\w): (\w*)/).captures # each () goes to the respective variable and is the captured in an array
if password.count(character).between?(low.to_i, high.to_i)
valid_passwords += 1
end
puts valid_passwords
endPython
"""
password-philosophy.py: from day_2.txt find out how many passwords are valid according to their policies; solve puzzle at https://adventofcode.com/2020/day/2
2 December 2020
@Vicki_Langer / @VickiLanger
"""
def validate_passwords():
num_valid_passwords = 0
for line in open('inputs/day_2.txt').readlines():
lower_upper, character, password = line.strip('\n').split()
character = character[0]
lower, upper = lower_upper.split('-')
count = password.count(character)
if (count >= int(lower)) & (count <= int(upper)):
num_valid_passwords += 1
print(num_valid_passwords)
validate_passwords()
1
Upvotes