-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.py
76 lines (66 loc) · 2.38 KB
/
day23.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
def cfg_as_str(config):
start = 1
next_cup = start
res = []
while True:
next_cup = config[next_cup][1]
if next_cup == start:
return "".join([str(num) for num in res])
res.append(next_cup)
def simulate(config, num_moves, current):
num_cups = len(config)
for _ in range(num_moves):
prev, next1 = config[current]
next2 = config[next1][1]
next3 = config[next2][1]
config[current] = [prev, config[next3][1]]
picked_up = [next1, next2, next3]
dest = current - 1
while True:
if dest <= 0:
dest = num_cups
if dest not in picked_up:
dest_prev, dest_next = config[dest]
config[dest] = [dest_prev, next1]
config[next1] = [dest, next2]
config[next2] = [next1, next3]
config[next3] = [next2, dest_next]
break
dest -= 1
current = config[current][1]
return config
def part1():
NUM_MOVES = 100
config_list = [int(num) for num in list("962713854")]
config = {}
for i in range(len(config_list)):
if i == len(config_list) - 1:
config[config_list[i]] = [config_list[i-1], config_list[0]]
elif i == 0:
config[config_list[i]] = [config_list[-1], config_list[i+1]]
else:
config[config_list[i]] = [config_list[i-1], config_list[i+1]]
simulate(config, NUM_MOVES, config_list[0])
return cfg_as_str(config)
def part2():
NUM_CUPS = 1000000
NUM_MOVES = 10000000
config_list = [int(num) for num in list("962713854")]
config = {} # store what number is prev and next in the ordering
for i in range(1, len(config_list)):
if i == len(config_list) - 1:
config[config_list[i]] = [config_list[i-1], len(config_list) + 1]
else:
config[config_list[i]] = [config_list[i-1], config_list[i+1]]
config[len(config_list)+1] = [config_list[-1], len(config_list)+2]
for i in range(len(config_list)+2, NUM_CUPS):
config[i] = [i-1, i+1]
config[NUM_CUPS] = [NUM_CUPS-1, config_list[0]]
config[config_list[0]] = [NUM_CUPS, config_list[1]]
simulate(config, NUM_MOVES, config_list[0])
next1 = config[1][1]
next2 = config[next1][1]
return next1 * next2
if __name__ == "__main__":
print(part1())
print(part2())