advent-of-code/2020/day2/part2.py
2024-12-03 15:27:19 +01:00

33 lines
797 B
Python

#! /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')