Advent of code

 19 décembre 2024 

  Linen Layout : Compter le nombre de façons de découper une chaîne en sous-chaîne autorisées.
gwurrw, guuu, bgguuu,
   
  
   
bwwwggur
   
buwwugww
   
wbrbwgub
   
           
  1. code_part1.py
  2. code_part2.py
  3. code_part2_rapide.py
from functools import cache

with open("input.txt", 'r', encoding='utf-8') as f:
    lines = [line[:-1] for line in f.readlines()]

patterns = lines[0].split(', ')
lines = lines[2:]

@cache
def nb_matches(line: str) -> int:
    if not line:
        return 1
    nb = 0
    for pat in patterns:
        if line.startswith(pat):
            nb += nb_matches(line[len(pat):])
    return nb

nb1 = 0
nb2 = 0

for line in lines:
    if n := nb_matches(line):
        nb1 += 1
        nb2 += n

## matches_tab = [n for n in (nb_matches(line) for line in lines) if n]
## nb1 = len(matches_tab)
## nb2 = sum(matches_tab)

print("Réponse partie 1:", nb1)
print("Réponse partie 2:", nb2)