Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def process1(s, part2=False):
- lines = s.strip('\n').split('\n')
- start = lines[0]
- pairs = collections.Counter(start[i:i+2] for i in range(len(start) - 1))
- rules = dict(rule.split(' -> ') for rule in lines[2:])
- for step in range(40 if part2 else 10):
- new_pairs = collections.Counter()
- for pair, count in pairs.items():
- new_pairs[pair[0] + rules[pair]] += count
- new_pairs[rules[pair] + pair[1]] += count
- pairs = new_pairs
- letters_in_pairs = collections.Counter()
- for pair, count in pairs.items():
- for letter in pair:
- letters_in_pairs[letter] += count
- letters_in_pairs[start[0]] += 1
- letters_in_pairs[start[-1]] += 1
- return (max(letters_in_pairs.values()) - min(letters_in_pairs.values())) // 2
Add Comment
Please, Sign In to add comment