Add day02 2020
This commit is contained in:
parent
e2b5e9487e
commit
479da8966f
50
2020/day02.py
Executable file
50
2020/day02.py
Executable file
@ -0,0 +1,50 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
"""
|
||||||
|
Jour 02 du défi Advent Of Code pour l'année 2020
|
||||||
|
"""
|
||||||
|
|
||||||
|
def read_sample():
|
||||||
|
"""récupère les entrées depuis le fichier texte correspondant"""
|
||||||
|
with open('inputs/day02.txt', 'r') as f:
|
||||||
|
sample = f.read().split('\n')
|
||||||
|
sample = [i for i in sample if i != '']
|
||||||
|
return sample
|
||||||
|
|
||||||
|
def xor(a, b):
|
||||||
|
return (a or b) and not (a and b)
|
||||||
|
|
||||||
|
def is_valid_part1(line):
|
||||||
|
crit, password = line.split(":")
|
||||||
|
number, letter = crit.split(" ")
|
||||||
|
mini, maxi = number.split("-")
|
||||||
|
return int(mini) <= password.count(letter) <= int(maxi)
|
||||||
|
|
||||||
|
def is_valid_part2(line):
|
||||||
|
crit, password = line.split(":")
|
||||||
|
number, letter = crit.split(" ")
|
||||||
|
pos1, pos2 = number.split("-")
|
||||||
|
password = password[1:]
|
||||||
|
return xor(password[int(pos1)-1] == letter, password[int(pos2)-1] == letter)
|
||||||
|
|
||||||
|
def part1(sample):
|
||||||
|
"""Partie 1 du défi"""
|
||||||
|
count = 0
|
||||||
|
for password in sample:
|
||||||
|
if is_valid_part1(password):
|
||||||
|
count += 1
|
||||||
|
return count
|
||||||
|
|
||||||
|
def part2(sample):
|
||||||
|
"""Partie 2 du défi"""
|
||||||
|
count = 0
|
||||||
|
for password in sample:
|
||||||
|
if is_valid_part2(password):
|
||||||
|
count += 1
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Fonction principale"""
|
||||||
|
sample = read_sample()
|
||||||
|
print(f"part1: {part1(sample)}")
|
||||||
|
print(f"part2: {part2(sample)}")
|
1000
2020/inputs/day02.txt
Normal file
1000
2020/inputs/day02.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user