advent/2020/2/solution.py

51 lines
1.1 KiB
Python
Raw Normal View History

2021-07-08 00:24:59 -05:00
#!/usr/bin/python
fd = open('input', 'r')
lines = fd.readlines()
def checka(minimum, maximum, letter, string):
cnt = 0
for i in string:
if i == letter: cnt += 1
if cnt >= minimum and cnt <= maximum:
return True
return False
def checkb(minimum, maximum, letter, string):
cnt = 0
if string[minimum - 1] == letter: cnt += 1
if string[maximum - 1] == letter: cnt += 1
if cnt == 1: return True
return False
def parse(line):
colon = line.split(':')
space = colon[0].split(' ')
dash = space[0].split('-')
minimum = int(dash[0])
maximum = int(dash[1])
letter = space[1]
string = colon[1].lstrip().rstrip()
return (minimum, maximum, letter, string)
def solutiona():
cnt = 0
for i in lines:
parsed = parse(i)
if checka(parsed[0], parsed[1], parsed[2], parsed[3]): cnt += 1
print(str(cnt))
def solutionb():
cnt = 0
for i in lines:
parsed = parse(i)
if checkb(parsed[0], parsed[1], parsed[2], parsed[3]): cnt += 1
print(str(cnt))
solutiona()
solutionb()