Skip to content

Commit ed8a2e2

Browse files
committed
Improved performance of 2020-23
1 parent 843b6d1 commit ed8a2e2

File tree

2 files changed

+197
-60
lines changed

2 files changed

+197
-60
lines changed

2020/23-Crab Cups.py

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -65,86 +65,67 @@ def words(s: str):
6565

6666

6767
# -------------------------------- Actual code execution ----------------------------- #
68-
68+
string = puzzle_input.split("\n")[0]
6969

7070
if part_to_test == 1:
7171
moves = 100
72-
for string in puzzle_input.split("\n"):
73-
cups = [int(x) for x in string]
74-
75-
for i in range(moves):
76-
cur_cup = cups[0]
77-
pickup = cups[1:4]
78-
del cups[0:4]
79-
80-
try:
81-
dest_cup = max([x for x in cups if x < cur_cup])
82-
except:
83-
dest_cup = max([x for x in cups])
84-
cups[cups.index(dest_cup) + 1 : cups.index(dest_cup) + 1] = pickup
85-
cups.append(cur_cup)
86-
87-
print(cups)
88-
89-
pos1 = cups.index(1)
90-
puzzle_actual_result = "".join(map(str, cups[pos1 + 1 :] + cups[:pos1]))
72+
nb_cups = 9
73+
next_cup = int(string[0])
9174

9275
else:
9376
moves = 10 ** 7
9477
nb_cups = 10 ** 6
78+
next_cup = 10
9579

96-
class Cup:
97-
def __init__(self, val, next_cup=None):
98-
self.val = val
99-
self.next_cup = next_cup
10080

101-
string = puzzle_input.split("\n")[0]
102-
next_cup = None
103-
cups = {}
104-
for x in string[::-1]:
105-
cups[x] = Cup(x, next_cup)
106-
next_cup = cups[x]
81+
cups = {}
82+
for x in string[::-1]:
83+
cups[int(x)] = next_cup
84+
next_cup = int(x)
10785

108-
next_cup = cups[string[0]]
86+
if part_to_test == 2:
87+
next_cup = int(string[0])
10988
for x in range(nb_cups, 9, -1):
110-
cups[str(x)] = Cup(str(x), next_cup)
111-
next_cup = cups[str(x)]
89+
cups[x] = next_cup
90+
next_cup = x
11291

113-
cups[string[-1]].next_cup = cups["10"]
92+
cur_cup = int(string[0])
93+
for i in range(moves):
94+
# print ('----- Move', i+1)
95+
# print ('Current', cur_cup)
11496

115-
cur_cup = cups[string[0]]
116-
for i in range(1, moves + 1):
117-
# #print ('----- Move', i)
118-
# #print ('Current', cur_cup.val)
97+
cups_moved = [
98+
cups[cur_cup],
99+
cups[cups[cur_cup]],
100+
cups[cups[cups[cur_cup]]],
101+
]
102+
# print ('Moved cups', cups_moved)
119103

120-
cups_moved = [
121-
cur_cup.next_cup,
122-
cur_cup.next_cup.next_cup,
123-
cur_cup.next_cup.next_cup.next_cup,
124-
]
125-
cups_moved_val = [cup.val for cup in cups_moved]
126-
# #print ('Moved cups', cups_moved_val)
104+
cups[cur_cup] = cups[cups_moved[-1]]
127105

128-
cur_cup.next_cup = cups_moved[-1].next_cup
106+
dest_cup = cur_cup - 1
107+
while dest_cup in cups_moved or dest_cup <= 0:
108+
dest_cup -= 1
109+
if dest_cup <= 0:
110+
dest_cup = nb_cups
129111

130-
dest_cup_nr = int(cur_cup.val) - 1
131-
while str(dest_cup_nr) in cups_moved_val or dest_cup_nr <= 0:
132-
dest_cup_nr -= 1
133-
if dest_cup_nr <= 0:
134-
dest_cup_nr = nb_cups
135-
dest_cup = cups[str(dest_cup_nr)]
112+
# print ("Destination", dest_cup)
136113

137-
# #print ("Destination", dest_cup_nr)
114+
cups[cups_moved[-1]] = cups[dest_cup]
115+
cups[dest_cup] = cups_moved[0]
138116

139-
cups_moved[-1].next_cup = dest_cup.next_cup
140-
dest_cup.next_cup = cups_moved[0]
117+
cur_cup = cups[cur_cup]
141118

142-
cur_cup = cur_cup.next_cup
119+
if part_to_test == 1:
120+
text = ""
121+
cup = cups[1]
122+
while cup != 1:
123+
text += str(cup)
124+
cup = cups[cup]
143125

144-
puzzle_actual_result = int(cups["1"].next_cup.val) * int(
145-
cups["1"].next_cup.next_cup.val
146-
)
147-
# #puzzle_actual_result = cups[(pos1+1)%len(cups)] * cups[(pos1+2)%len(cups)]
126+
puzzle_actual_result = text
127+
else:
128+
puzzle_actual_result = cups[1] * cups[cups[1]]
148129

149130
# -------------------------------- Outputs / results --------------------------------- #
150131

2020/23-Crab Cups.v2.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools
3+
from collections import Counter, deque, defaultdict
4+
5+
from compass import *
6+
7+
# from simply_linked_list import *
8+
9+
10+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
11+
def lmap(func, *iterables):
12+
return list(map(func, *iterables))
13+
14+
15+
def ints(s: str):
16+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
17+
18+
19+
def positive_ints(s: str):
20+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
21+
22+
23+
def floats(s: str):
24+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
25+
26+
27+
def positive_floats(s: str):
28+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
29+
30+
31+
def words(s: str):
32+
return re.findall(r"[a-zA-Z]+", s)
33+
34+
35+
test_data = {}
36+
37+
test = 1
38+
test_data[test] = {
39+
"input": """389125467""",
40+
"expected": ["92658374 after 10 moves, 67384529 after 100 moves", "149245887792"],
41+
}
42+
43+
test = "real"
44+
input_file = os.path.join(
45+
os.path.dirname(__file__),
46+
"Inputs",
47+
os.path.basename(__file__).replace(".py", ".txt"),
48+
)
49+
test_data[test] = {
50+
"input": open(input_file, "r+").read(),
51+
"expected": ["45286397", "836763710"],
52+
}
53+
54+
55+
# -------------------------------- Control program execution ------------------------- #
56+
57+
case_to_test = 1
58+
part_to_test = 2
59+
60+
# -------------------------------- Initialize some variables ------------------------- #
61+
62+
puzzle_input = test_data[case_to_test]["input"]
63+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
64+
puzzle_actual_result = "Unknown"
65+
66+
67+
# -------------------------------- Actual code execution ----------------------------- #
68+
69+
70+
if part_to_test == 1:
71+
moves = 100
72+
for string in puzzle_input.split("\n"):
73+
cups = [int(x) for x in string]
74+
75+
for i in range(moves):
76+
cur_cup = cups[0]
77+
pickup = cups[1:4]
78+
del cups[0:4]
79+
80+
try:
81+
dest_cup = max([x for x in cups if x < cur_cup])
82+
except:
83+
dest_cup = max([x for x in cups])
84+
cups[cups.index(dest_cup) + 1 : cups.index(dest_cup) + 1] = pickup
85+
cups.append(cur_cup)
86+
87+
print(cups)
88+
89+
pos1 = cups.index(1)
90+
puzzle_actual_result = "".join(map(str, cups[pos1 + 1 :] + cups[:pos1]))
91+
92+
else:
93+
moves = 10 ** 7
94+
nb_cups = 10 ** 6
95+
96+
class Cup:
97+
def __init__(self, val, next_cup=None):
98+
self.val = val
99+
self.next_cup = next_cup
100+
101+
string = puzzle_input.split("\n")[0]
102+
next_cup = None
103+
cups = {}
104+
for x in string[::-1]:
105+
cups[x] = Cup(x, next_cup)
106+
next_cup = cups[x]
107+
108+
next_cup = cups[string[0]]
109+
for x in range(nb_cups, 9, -1):
110+
cups[str(x)] = Cup(str(x), next_cup)
111+
next_cup = cups[str(x)]
112+
113+
cups[string[-1]].next_cup = cups["10"]
114+
115+
cur_cup = cups[string[0]]
116+
for i in range(1, moves + 1):
117+
# #print ('----- Move', i)
118+
# #print ('Current', cur_cup.val)
119+
120+
cups_moved = [
121+
cur_cup.next_cup,
122+
cur_cup.next_cup.next_cup,
123+
cur_cup.next_cup.next_cup.next_cup,
124+
]
125+
cups_moved_val = [cup.val for cup in cups_moved]
126+
# #print ('Moved cups', cups_moved_val)
127+
128+
cur_cup.next_cup = cups_moved[-1].next_cup
129+
130+
dest_cup_nr = int(cur_cup.val) - 1
131+
while str(dest_cup_nr) in cups_moved_val or dest_cup_nr <= 0:
132+
dest_cup_nr -= 1
133+
if dest_cup_nr <= 0:
134+
dest_cup_nr = nb_cups
135+
dest_cup = cups[str(dest_cup_nr)]
136+
137+
# #print ("Destination", dest_cup_nr)
138+
139+
cups_moved[-1].next_cup = dest_cup.next_cup
140+
dest_cup.next_cup = cups_moved[0]
141+
142+
cur_cup = cur_cup.next_cup
143+
144+
puzzle_actual_result = int(cups["1"].next_cup.val) * int(
145+
cups["1"].next_cup.next_cup.val
146+
)
147+
# #puzzle_actual_result = cups[(pos1+1)%len(cups)] * cups[(pos1+2)%len(cups)]
148+
149+
# -------------------------------- Outputs / results --------------------------------- #
150+
151+
print("Case :", case_to_test, "- Part", part_to_test)
152+
print("Expected result : " + str(puzzle_expected_result))
153+
print("Actual result : " + str(puzzle_actual_result))
154+
# Date created: 2020-12-23 06:25:17.546310
155+
# Part 1: 2020-12-23 06:36:18
156+
# Part 2: 2020-12-23 15:21:48

0 commit comments

Comments
 (0)