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 solution for 2023/09

+39
+39
2023/day09.py
··· 1 + import fileinput 2 + 3 + 4 + part_1 = 0 5 + part_2 = 0 6 + 7 + for line in fileinput.input(): 8 + row = [int(x) for x in line.split()] 9 + 10 + # Build up triangle. 11 + triangle = [row[:]] 12 + 13 + while any(n != 0 for n in triangle[-1]): 14 + row = [] 15 + for a, b in zip(triangle[-1], triangle[-1][1:]): 16 + row.append(b - a) 17 + 18 + triangle.append(row) 19 + 20 + triangle.reverse() 21 + 22 + # Extrapolate right column. 23 + right = [0] 24 + for *_, n in triangle[1:]: 25 + right.append(right[-1] + n) 26 + 27 + part_1 += right[-1] 28 + 29 + # Extrapolate left column. 30 + left = [0] 31 + for n, *_ in triangle[1:]: 32 + left.append(n - left[-1]) 33 + 34 + part_2 += left[-1] 35 + 36 + 37 + print("Part 1:", part_1) 38 + print("Part 2:", part_2) 39 +