hhoppe

Advent of code 2021 day 14

Dec 14th, 2021 (edited)
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. def process1(s, part2=False):
  2.   lines = s.strip('\n').split('\n')
  3.   start = lines[0]
  4.   pairs = collections.Counter(start[i:i+2] for i in range(len(start) - 1))
  5.   rules = dict(rule.split(' -> ') for rule in lines[2:])
  6.  
  7.   for step in range(40 if part2 else 10):
  8.     new_pairs = collections.Counter()
  9.     for pair, count in pairs.items():
  10.       new_pairs[pair[0] + rules[pair]] += count
  11.       new_pairs[rules[pair] + pair[1]] += count
  12.     pairs = new_pairs
  13.  
  14.   letters_in_pairs = collections.Counter()
  15.   for pair, count in pairs.items():
  16.     for letter in pair:
  17.       letters_in_pairs[letter] += count
  18.  
  19.   letters_in_pairs[start[0]] += 1
  20.   letters_in_pairs[start[-1]] += 1
  21.   return (max(letters_in_pairs.values()) - min(letters_in_pairs.values())) // 2
  22.  
  23.  
Add Comment
Please, Sign In to add comment