day4 so far + formatz
This commit is contained in:
27
day2/day2.py
27
day2/day2.py
@@ -1,6 +1,7 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
def validate_password(line):
|
||||
parts = re.split('([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)', line)
|
||||
lower = int(parts[1])
|
||||
@@ -8,11 +9,12 @@ def validate_password(line):
|
||||
char = parts[3]
|
||||
|
||||
password = parts[4]
|
||||
password = re.sub('[^%s]'% char, '', parts[4])
|
||||
password = re.sub('[^%s]' % char, '', parts[4])
|
||||
length = len(password)
|
||||
if(length >= lower and length <= upper):
|
||||
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
|
||||
@@ -21,25 +23,28 @@ def validate_password_2(line):
|
||||
|
||||
password = parts[4]
|
||||
|
||||
if((password[lower] is char) is not (password[upper] is char)):
|
||||
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:
|
||||
with open("day2/input.txt") as input:
|
||||
for line in input:
|
||||
if validate_password(line) == True:
|
||||
count += 1
|
||||
print("Day 1: %d"% count)
|
||||
|
||||
print("Day 1: %d" % count)
|
||||
|
||||
|
||||
def part2():
|
||||
count = 0
|
||||
with open("day2/input.txt") as input:
|
||||
for line in input:
|
||||
with open("day2/input.txt") as input:
|
||||
for line in input:
|
||||
if validate_password_2(line) == True:
|
||||
count += 1
|
||||
print("Day 2: %d"% count)
|
||||
print("Day 2: %d" % count)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part1();
|
||||
part2();
|
||||
part2();
|
||||
|
||||
Reference in New Issue
Block a user