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 2021/13

+45
+45
2021/day13.py
··· 1 + import fileinput 2 + from utils import print_grid, Point 3 + 4 + 5 + DOTS = {} 6 + FOLDS = [] 7 + 8 + 9 + # Read problem input 10 + data = ''.join([line for line in fileinput.input()]) 11 + groups = [g.split('\n') for g in data.split('\n\n')] 12 + 13 + for line in groups[0]: 14 + x, y = [int(x) for x in line.split(',')] 15 + DOTS[Point(x, y)] = '#' 16 + 17 + for line in groups[1]: 18 + axis, n = line.split()[2].split('=') 19 + FOLDS.append((axis, int(n))) 20 + 21 + for i, (axis, n) in enumerate(FOLDS): 22 + new_dots = {} 23 + 24 + for p in DOTS: 25 + if axis == 'x': 26 + if p.x < n: 27 + new_dots[p] = '#' 28 + else: 29 + dx = abs(n - p.x) 30 + nx = n - dx 31 + new_dots[Point(nx, p.y)] = '#' 32 + else: 33 + if p.y < n: 34 + new_dots[p] = '#' 35 + else: 36 + dy = abs(n - p.y) 37 + ny = n - dy 38 + new_dots[Point(p.x, ny)] = '#' 39 + 40 + DOTS = new_dots 41 + 42 + if i == 0: 43 + print "Part 1:", len(DOTS) 44 + 45 + print "Part 2:\n", '\n'.join(print_grid(DOTS, quiet=True))