-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday16_dancing.py
97 lines (69 loc) · 3.05 KB
/
day16_dancing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Advent of Code -- Day 16
http://adventofcode.com/2017/day/16"""
from typing import Dict, List
def create_initial_position(num_programs: int) -> List[str]:
"""a stats at 0 and so on until we hit num_programs"""
letter_a_position = ord('a')
starting_grid = []
for letter in range(letter_a_position, letter_a_position + num_programs):
starting_grid.append(chr(letter))
return starting_grid
def spin(positions: List[str], x) -> List[str]:
"""Makes X programs move from the end to the front"""
new_positions = list(positions[-x:]) + list(positions[:-x])
return new_positions
def exchange(positions: List[str], swap1: int, swap2: int) -> List[str]:
"""makes the programs at positions A and B swap places"""
new_positions = list(positions)
new_positions[swap1], new_positions[swap2] = \
new_positions[swap2], new_positions[swap1]
return new_positions
def parnter(positions: List[str], item1: str, item2: str) -> List[str]:
"""makes the programs named A and B swap places"""
position1 = positions.index(item1)
position2 = positions.index(item2)
return exchange(positions, position1, position2)
def dance_puppets(position: List[str], moves: List[str]) -> List[str]:
dance_position = list(position)
for move in all_moves:
action = move[:1]
remaining_move = move[1:]
if action == 's':
dance_position = spin(dance_position, int(remaining_move))
elif action == 'x':
pos1, pos2 = remaining_move.split('/')
dance_position = exchange(dance_position, int(pos1), int(pos2))
elif action == 'p':
item1, item2 = remaining_move.split('/')
dance_position = parnter(dance_position, item1, item2)
return dance_position
def get_pattern(positions: List[str],
moves: List[str],
num_dances: int) -> Dict[int, str]:
"""Find pattern"""
dance_position = list(position)
pattern_finder = {}
for count in range(num_dances):
dance_position = dance_puppets(dance_position, moves)
pattern_finder[count + 1] = ''.join(dance_position)
return pattern_finder
if __name__ == '__main__':
TEST_NUM_PROGRAMS = 5
TEST_START_POSITION = create_initial_position(TEST_NUM_PROGRAMS)
assert spin(TEST_START_POSITION, 3) == ['c', 'd', 'e', 'a', 'b']
first_move = spin(TEST_START_POSITION, 1)
assert first_move == ['e', 'a', 'b', 'c', 'd']
second_move = exchange(first_move, 3, 4)
assert second_move == ['e', 'a', 'b', 'd', 'c']
third_move = parnter(second_move, 'e', 'b')
assert third_move == ['b', 'a', 'e', 'd', 'c']
# read in input
all_moves: List[str] = []
with open('day16_input.txt', 'r') as f:
all_moves = f.readline().strip().split(',')
position = create_initial_position(16)
final_position = dance_puppets(position, all_moves)
print(''.join(final_position))
# using trial and error we see pattern repeats every 30
pattern = get_pattern(position, all_moves, 30)
print(pattern[1_000_000_000 % 30])