philRG

aoc day 14

Dec 14th, 2021 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. """ --- Day 14: Extended Polymerization ---
  2. https://adventofcode.com/2021/day/14#part2
  3. https://culturesciences.chimie.ens.fr/thematiques/chimie-des-materiaux/polymeres/materiaux-polymeres-architecture-macromoleculaire
  4. """
  5. import time
  6.  
  7. """ philRG tu n'es pas obligé de faire effectivement les insertions proposées, tu peux juste faire un "bilan" des lettres et des paires présentes
  8. """
  9.  
  10. STEPS_COUNT = 40
  11.  
  12.  
  13. def count_insertions(p1, p2, step=1):
  14.     global count_mol
  15.     if step > STEPS_COUNT:
  16.         return
  17.     insert_mol = pairs[(p1, p2)]
  18.     count_mol[insert_mol] += 1
  19.     count_insertions(p1, insert_mol, step + 1)
  20.     count_insertions(insert_mol, p2, step + 1)
  21.  
  22.  
  23. tic = time.time()
  24.  
  25. polymer = list(input())
  26.  
  27. polymer_str = ''.join(polymer)
  28. print(f'polymer template = {polymer_str}')
  29.  
  30. blank_line = input()
  31.  
  32. pairs = {}
  33. while True:
  34.     try:
  35.         pair, insert = input().split(' -> ')
  36.         a, b = list(pair)
  37.         pairs[(a, b)] = insert
  38.     except EOFError:
  39.         break
  40.  
  41. molecules = set(polymer)
  42. count_mol = {mol: 0 for mol in sorted(molecules)} | {'S': 0}
  43. for mol in polymer:
  44.     count_mol[mol] += 1
  45.    
  46. print(count_mol)
  47. for i in range(len(polymer) - 1):
  48.     p1, p2 = polymer[i], polymer[i + 1]
  49.     count_insertions(p1, p2)
  50.  
  51. least_common_mol = min(count_mol.items(), key=lambda x: x[1])
  52. most_common_mol = max(count_mol.items(), key=lambda x: x[1])
  53.  
  54. print(f'least_common_mol = {least_common_mol[0]}[{least_common_mol[1]}] - most_common_mol = {most_common_mol[0]}[{most_common_mol[1]}]')
  55.  
  56. result = most_common_mol[1] - least_common_mol[1]
  57.  
  58. elapsed_time = time.time() - tic
  59. seconds = round(elapsed_time, 3)
  60.  
  61. print(f'result = {result} for {STEPS_COUNT} steps in {seconds} seconds')
  62.  
Add Comment
Please, Sign In to add comment