My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2022/20

+96
+96
2022/day20.py
··· 1 + import fileinput 2 + 3 + 4 + class Node: 5 + def __init__(self, i, val): 6 + self.i = i 7 + self.val = val 8 + self.back = None 9 + self.next = None 10 + 11 + 12 + # Parse problem input. 13 + INPUT = [int(x) for x in fileinput.input()] 14 + 15 + 16 + def construct_problem(problem_input, decryption_key=1): 17 + numbers = [] 18 + first = None 19 + last = None 20 + zero = None 21 + for i, num in enumerate(problem_input): 22 + node = Node(i, num * decryption_key) 23 + 24 + if num == 0: 25 + zero = node 26 + 27 + if first is None: 28 + first = node 29 + else: 30 + node.back = last 31 + 32 + if last is not None: 33 + last.next = node 34 + 35 + last = node 36 + numbers.append(node) 37 + 38 + last.next = first 39 + first.back = last 40 + 41 + return numbers, zero 42 + 43 + 44 + def move_number(node, mod): 45 + n = node.val % mod 46 + if n == 0: 47 + return 48 + 49 + # Keep track of old parent/child. 50 + parent = node.back 51 + child = node.next 52 + 53 + # Find the next location. 54 + curr = node 55 + for _ in range(n): 56 + curr = curr.next 57 + 58 + # Point new parent/child to node and vice versa. 59 + desc = curr.next 60 + 61 + desc.back = node 62 + curr.next = node 63 + node.back = curr 64 + node.next = desc 65 + 66 + # Put the old parent/child together. 67 + parent.next = child 68 + child.back = parent 69 + 70 + 71 + def mix(numbers): 72 + for n in numbers: 73 + move_number(n, len(numbers) - 1) 74 + 75 + 76 + def get_grove_coordinates(curr): 77 + vals = [] 78 + for i in range(3): 79 + for _ in range(1000): 80 + curr = curr.next 81 + 82 + vals.append(curr.val) 83 + 84 + return sum(vals) 85 + 86 + 87 + # Solve part 1. 88 + numbers, zero = construct_problem(INPUT) 89 + mix(numbers) 90 + print("Part 1:", get_grove_coordinates(zero)) 91 + 92 + # Solve part 2. 93 + numbers, zero = construct_problem(INPUT, decryption_key=811589153) 94 + for _ in range(10): mix(numbers) 95 + print("Part 2:", get_grove_coordinates(zero)) 96 +