Add day01 2021
This commit is contained in:
parent
479da8966f
commit
3dd31b7a37
39
2021/day01.py
Executable file
39
2021/day01.py
Executable file
@ -0,0 +1,39 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
"""
|
||||||
|
Jour 01 du défi Advent Of Code pour l'année 2021
|
||||||
|
"""
|
||||||
|
|
||||||
|
def read_sample():
|
||||||
|
"""récupère les entrées depuis le fichier texte correspondant"""
|
||||||
|
with open('inputs/day01.txt', 'r') as f:
|
||||||
|
sample = f.read().split('\n')
|
||||||
|
sample = [int(i) for i in sample if i != '']
|
||||||
|
return sample
|
||||||
|
|
||||||
|
|
||||||
|
def increased(sample):
|
||||||
|
"""renvoie le nombre de fois que l'échantillon a augmenté"""
|
||||||
|
depth = sample[0]
|
||||||
|
depth_incr = 0
|
||||||
|
for i in sample:
|
||||||
|
if i > depth:
|
||||||
|
depth_incr += 1
|
||||||
|
depth = i
|
||||||
|
|
||||||
|
return depth_incr
|
||||||
|
|
||||||
|
def part1(sample):
|
||||||
|
"""Partie 1 du défi"""
|
||||||
|
return increased(sample)
|
||||||
|
|
||||||
|
def part2(sample):
|
||||||
|
"""Partie 2 du défi"""
|
||||||
|
sum_of_measurements = [sample[i]+sample[i+1]+sample[i+2] for i in range(len(sample)-2)]
|
||||||
|
return increased(sum_of_measurements)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Fonction principale"""
|
||||||
|
sample = read_sample()
|
||||||
|
print(f"part1: {part1(sample)}")
|
||||||
|
print(f"part2: {part2(sample)}")
|
2000
2021/inputs/day01.txt
Normal file
2000
2021/inputs/day01.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user