advent-of-code/2023/day09.py

57 lines
1.3 KiB
Python
Raw Normal View History

2023-12-09 10:12:35 +01:00
#!/usr/bin/python3
"""
Jour 09 du défi Advent Of Code pour l'année 2023
"""
import os
2023-12-09 10:12:35 +01:00
def read_sample():
"""récupère les entrées depuis le fichier texte correspondant"""
filename = os.path.join(os.path.dirname(__file__ ), "inputs", "day09.txt")
with open(filename, 'r') as f:
2023-12-09 10:12:35 +01:00
sample = f.read().split('\n')
sample = [ [int(j) for j in i.split()] for i in sample if i != '' ]
return sample
def differences(seq):
return [seq[i+1]-seq[i] for i in range(len(seq)-1)]
def null_list(seq):
return False not in [i==0 for i in seq]
def extrapolate(seq, rev=False):
if rev:
seq = list(reversed(seq))
seqs = [seq]
while not null_list(seqs[-1]):
seqs.append(differences(seqs[-1]))
seqs[-1].append(0)
for i in range(len(seqs)-1):
seq = seqs[len(seqs)-2-i]
prev = seqs[len(seqs)-i-1]
seqs[len(seqs)-2-i].append(seq[-1]+prev[-1])
return seqs
def part1(sample):
"""Partie 1 du défi"""
return sum([extrapolate(seq, rev=False)[0][-1] for seq in sample])
def part2(sample):
"""Partie 2 du défi"""
return sum([extrapolate(seq, rev=True)[0][-1] for seq in sample])
def main():
"""Fonction principale"""
sample = read_sample()
print(f"part1: {part1(sample)}")
print(f"part2: {part2(sample)}")
if __name__ == "__main__":
main()