day2 challenge

This commit is contained in:
Thibaud Gasser 2020-12-02 10:53:26 +01:00
parent 808a1440de
commit bac680a0a0
3 changed files with 1060 additions and 0 deletions

1000
day2/input.txt Normal file

File diff suppressed because it is too large Load Diff

28
day2/part1.py Normal file
View File

@ -0,0 +1,28 @@
#! /usr/bin/env python3
def parse_line(line):
repeat_range, letter, pwd = line.split(' ')
letter = letter[0]
repeat_min, repeat_max = repeat_range.split('-')
repeat_min, repeat_max = int(repeat_min), int(repeat_max)
return letter, range(repeat_min, repeat_max + 1), pwd
def test_password(line):
letter, repeat_range, pwd = parse_line(line)
count = pwd.count(letter)
return count in repeat_range
def main(inp):
with open(inp) as passwds:
valid_counter = 0
for l in passwds:
if test_password(l):
valid_counter += 1
print(f"Number of valid password in input : {valid_counter}")
if __name__ == "__main__":
main('./input.txt')

32
day2/part2.py Normal file
View File

@ -0,0 +1,32 @@
#! /usr/bin/env python3
def xor(a, b):
return (a and not b) or (not a and b)
def parse_line(line):
repeat_range, letter, pwd = line.split(' ')
letter = letter[0]
first_pos, second_pos = repeat_range.split('-')
first_pos, second_pos = int(first_pos), int(second_pos)
return letter, first_pos, second_pos, pwd
def test_password(line):
letter, first_pos, second_pos, pwd = parse_line(line)
return xor(pwd[first_pos - 1] == letter,
pwd[second_pos - 1] == letter)
def main(inp):
with open(inp) as passwds:
valid_counter = 0
for l in passwds:
if test_password(l):
valid_counter += 1
print(f"Number of valid password in input : {valid_counter}")
if __name__ == "__main__":
main('./input.txt')