Add 2023 day 01
This commit is contained in:
parent
040b3d525c
commit
33da7d8093
63
2023/day01.py
Executable file
63
2023/day01.py
Executable file
@ -0,0 +1,63 @@
|
||||
#!/usr/bin/python3
|
||||
"""
|
||||
Jour 01 du défi Advent Of Code pour l'année 2023
|
||||
"""
|
||||
|
||||
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 = [ i for i in sample if i != '' ]
|
||||
return sample
|
||||
|
||||
def number(word):
|
||||
nbs = (0, 0)
|
||||
seen = 0
|
||||
for i in word:
|
||||
if i >= '0' and i <= '9':
|
||||
if seen == 0:
|
||||
nbs = (int(i), int(i))
|
||||
seen += 1
|
||||
else:
|
||||
nbs = (nbs[0], int(i))
|
||||
return 10*nbs[0]+nbs[1]
|
||||
|
||||
def part1(sample):
|
||||
"""Partie 1 du défi"""
|
||||
return sum([
|
||||
number(word) for word in sample
|
||||
])
|
||||
|
||||
def treat_input(sample):
|
||||
pairs = [
|
||||
("one", "o1e"),
|
||||
("two", "t2o"),
|
||||
("three", "th3ee"),
|
||||
("four", "fo4r"),
|
||||
("five", "f5ve"),
|
||||
("six", "s6x"),
|
||||
("seven", "se7en"),
|
||||
("eight", "eig8ht"),
|
||||
("nine", "ni9ne"),
|
||||
("zero", "ze0ro")
|
||||
]
|
||||
for base, rep in pairs:
|
||||
sample = [i.replace(base, rep) for i in sample]
|
||||
|
||||
return sample
|
||||
|
||||
def part2(sample):
|
||||
"""Partie 2 du défi"""
|
||||
return sum([
|
||||
number(word) for word in treat_input(sample)
|
||||
])
|
||||
|
||||
|
||||
def main():
|
||||
"""Fonction principale"""
|
||||
sample = read_sample()
|
||||
print(f"part1: {part1(sample)}")
|
||||
print(f"part2: {part2(sample)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user