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 2016/21

+189
+87
2016/day21.py
··· 1 + import fileinput 2 + from collections import deque 3 + 4 + from utils import parse_line 5 + 6 + 7 + def scramble(data, operations, unscramble=False): 8 + data = deque(data) 9 + 10 + if unscramble: 11 + operations.reverse() 12 + 13 + for line in operations: 14 + if line.startswith('swap position'): 15 + x, y = parse_line(r'swap position (\d+) with position (\d+)', line) 16 + data[x], data[y] = data[y], data[x] 17 + 18 + elif line.startswith('swap letter'): 19 + x, y = parse_line(r'swap letter (\w+) with letter (\w+)', line) 20 + data = deque(''.join(data).replace(x, '_').replace(y, x).replace('_', y)) 21 + 22 + elif line.startswith('rotate based'): 23 + x = line.split()[-1] 24 + tmp = ''.join(data) 25 + 26 + if not unscramble: 27 + i = tmp.index(x) 28 + data.rotate(i + (2 if i >= 4 else 1)) 29 + 30 + else: 31 + # Keep trying rotation candidates until one is valid 32 + curr = deque(data) 33 + 34 + while True: 35 + curr.rotate(1) 36 + post = ''.join(curr) 37 + idx = post.index(x) 38 + 39 + tmp = deque(data) 40 + tmp.rotate(-(idx + (2 if idx >= 4 else 1))) 41 + pre = ''.join(tmp) 42 + 43 + if pre == post: 44 + data = deque(pre) 45 + break 46 + 47 + 48 + elif line.startswith('rotate'): 49 + lr, x = parse_line(r'rotate (\w+) (\d+) step', line) 50 + 51 + if (unscramble and lr == 'right') or (not unscramble and lr == 'left'): 52 + x = -x 53 + 54 + data.rotate(x) 55 + 56 + elif line.startswith('reverse'): 57 + x, y = parse_line(r'reverse positions (\d+) through (\d+)', line) 58 + 59 + tmp = ''.join(data) 60 + tmp = tmp[:x] + ''.join(reversed(tmp[x:y+1])) + tmp[y+1:] 61 + data = deque(tmp) 62 + 63 + elif line.startswith('move'): 64 + x, y = parse_line(r'move position (\d+) to position (\d+)', line) 65 + 66 + if unscramble: 67 + x, y = y, x 68 + 69 + tmp = ''.join(data) 70 + 71 + # Extract the character 72 + c = tmp[x] 73 + tmp = tmp[:x] + tmp[x+1:] 74 + 75 + # Reinsert it 76 + tmp = tmp[:y] + c + tmp[y:] 77 + 78 + data = deque(tmp) 79 + 80 + 81 + return ''.join(data) 82 + 83 + 84 + if __name__ == '__main__': 85 + operations = [line.strip() for line in fileinput.input()] 86 + print 'Result of scrambling abcdefgh:', scramble('abcdefgh', operations, False) 87 + print 'Unscrambled version of fbgdceah:', scramble('fbgdceah', operations, unscramble=True)
+100
2016/inputs/21.txt
··· 1 + move position 2 to position 6 2 + move position 0 to position 5 3 + move position 6 to position 4 4 + reverse positions 3 through 7 5 + move position 1 to position 7 6 + swap position 6 with position 3 7 + swap letter g with letter b 8 + swap position 2 with position 3 9 + move position 4 to position 3 10 + move position 6 to position 3 11 + swap position 4 with position 1 12 + swap letter b with letter f 13 + reverse positions 3 through 4 14 + swap letter f with letter e 15 + reverse positions 2 through 7 16 + rotate based on position of letter h 17 + rotate based on position of letter a 18 + rotate based on position of letter e 19 + rotate based on position of letter h 20 + rotate based on position of letter c 21 + move position 5 to position 7 22 + swap letter a with letter d 23 + move position 5 to position 6 24 + swap position 4 with position 0 25 + swap position 4 with position 6 26 + rotate left 6 steps 27 + rotate right 4 steps 28 + rotate right 5 steps 29 + swap letter f with letter e 30 + swap position 2 with position 7 31 + rotate based on position of letter e 32 + move position 4 to position 5 33 + swap position 4 with position 2 34 + rotate right 1 step 35 + swap letter b with letter f 36 + rotate based on position of letter b 37 + reverse positions 3 through 5 38 + move position 3 to position 1 39 + rotate based on position of letter g 40 + swap letter c with letter e 41 + swap position 7 with position 3 42 + move position 0 to position 3 43 + rotate right 6 steps 44 + reverse positions 1 through 3 45 + swap letter d with letter e 46 + reverse positions 3 through 5 47 + move position 0 to position 3 48 + swap letter c with letter e 49 + move position 2 to position 7 50 + swap letter g with letter b 51 + rotate right 0 steps 52 + reverse positions 1 through 3 53 + swap letter h with letter d 54 + move position 4 to position 0 55 + move position 6 to position 3 56 + swap letter a with letter c 57 + reverse positions 3 through 6 58 + swap letter h with letter g 59 + move position 7 to position 2 60 + rotate based on position of letter h 61 + swap letter b with letter h 62 + reverse positions 2 through 6 63 + move position 6 to position 7 64 + rotate based on position of letter a 65 + rotate right 7 steps 66 + reverse positions 1 through 6 67 + move position 1 to position 6 68 + rotate based on position of letter g 69 + rotate based on position of letter d 70 + move position 0 to position 4 71 + rotate based on position of letter e 72 + rotate based on position of letter d 73 + rotate based on position of letter a 74 + rotate based on position of letter a 75 + rotate right 4 steps 76 + rotate based on position of letter b 77 + reverse positions 0 through 4 78 + move position 1 to position 7 79 + rotate based on position of letter e 80 + move position 1 to position 7 81 + swap letter f with letter h 82 + move position 5 to position 1 83 + rotate based on position of letter f 84 + reverse positions 0 through 1 85 + move position 2 to position 4 86 + rotate based on position of letter a 87 + swap letter b with letter d 88 + move position 6 to position 0 89 + swap letter e with letter b 90 + rotate right 7 steps 91 + move position 2 to position 7 92 + rotate left 4 steps 93 + swap position 6 with position 1 94 + move position 3 to position 5 95 + rotate right 7 steps 96 + reverse positions 0 through 6 97 + swap position 2 with position 1 98 + reverse positions 4 through 6 99 + rotate based on position of letter g 100 + move position 6 to position 4
+2
2016/outputs/21.txt
··· 1 + dgfaehcb 2 + fdhgacbe