51 lines
1.1 KiB
Python
51 lines
1.1 KiB
Python
import os
|
|
import re
|
|
|
|
|
|
def validate_password(line):
|
|
parts = re.split('([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', line)
|
|
lower = int(parts[1])
|
|
upper = int(parts[2])
|
|
char = parts[3]
|
|
|
|
password = parts[4]
|
|
password = re.sub('[^%s]' % char, '', parts[4])
|
|
length = len(password)
|
|
if (length >= lower and length <= upper):
|
|
return True
|
|
|
|
|
|
def validate_password_2(line):
|
|
parts = re.split('([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', line)
|
|
lower = int(parts[1]) - 1
|
|
upper = int(parts[2]) - 1
|
|
char = parts[3]
|
|
|
|
password = parts[4]
|
|
|
|
if ((password[lower] is char) is not (password[upper] is char)):
|
|
return True
|
|
|
|
|
|
def part1():
|
|
count = 0
|
|
with open("day2/input.txt") as input:
|
|
for line in input:
|
|
if validate_password(line) == True:
|
|
count += 1
|
|
print("Day 1: %d" % count)
|
|
|
|
|
|
def part2():
|
|
count = 0
|
|
with open("day2/input.txt") as input:
|
|
for line in input:
|
|
if validate_password_2(line) == True:
|
|
count += 1
|
|
print("Day 2: %d" % count)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
part1();
|
|
part2();
|