Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- def get_stones(file):
- values = [int(x) for x in open(file).read().strip().split(' ')]
- dictionary = defaultdict(int)
- for i in range(len(values)):
- dictionary[values[i]] = 1
- return dictionary
- def evolve(stones):
- output = defaultdict(int)
- for mark, quantity in stones.items():
- if mark == 0: # rule 1
- output[1] += quantity
- elif len(str(mark)) % 2 == 0: # rule 2
- midpoint = len(str(mark)) // 2
- left = int(str(mark)[:midpoint])
- right = int(str(mark)[midpoint:])
- output[left] += quantity
- output[right] += quantity
- else: # rule 3
- output[mark * 2024] += quantity
- return output
- def main(file, times):
- stones = get_stones(file)
- for i in range(times):
- stones = evolve(stones)
- return sum(stones.values())
- if __name__ == '__main__':
- print(main('input.txt', 25))
- print(main('input.txt', 75))
Advertisement
Add Comment
Please, Sign In to add comment