Content-Length: 687646 | pFad | http://github.com/Piratmac/adventofcode-python/commit/d737eb6de1973d2cbeefa4ff44762167a3a02fce

91 Further performance improvement · Piratmac/adventofcode-python@d737eb6 · GitHub
Skip to content

Commit d737eb6

Browse files
committed
Further performance improvement
1 parent 8fcb2d3 commit d737eb6

File tree

2 files changed

+143
-51
lines changed

2 files changed

+143
-51
lines changed

2016/12-Leonardo's Monorail.py

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,89 @@
44
test_data = {}
55

66
test = 1
7-
test_data[test] = {"input": """cpy 41 a
7+
test_data[test] = {
8+
"input": """cpy 41 a
89
inc a
910
inc a
1011
dec a
1112
jnz a 2
1213
dec a""",
13-
"expected": ['42', 'Unknown'],
14-
}
14+
"expected": ["42", "Unknown"],
15+
}
1516

1617
test += 1
17-
test_data[test] = {"input": """""",
18-
"expected": ['Unknown', 'Unknown'],
19-
}
20-
21-
test = 'real'
22-
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
23-
test_data[test] = {"input": open(input_file, "r+").read().strip(),
24-
"expected": ['318083', '9227737'],
25-
}
18+
test_data[test] = {
19+
"input": """""",
20+
"expected": ["Unknown", "Unknown"],
21+
}
22+
23+
test = "real"
24+
input_file = os.path.join(
25+
os.path.dirname(__file__),
26+
"Inputs",
27+
os.path.basename(__file__).replace(".py", ".txt"),
28+
)
29+
test_data[test] = {
30+
"input": open(input_file, "r+").read().strip(),
31+
"expected": ["318083", "9227737"],
32+
}
2633

2734
# -------------------------------- Control program execution -------------------------------- #
2835

29-
case_to_test = 'real'
30-
part_to_test = 2
36+
case_to_test = "real"
37+
part_to_test = 2
3138
verbose_level = 1
3239

3340
# -------------------------------- Initialize some variables -------------------------------- #
3441

35-
puzzle_input = test_data[case_to_test]['input']
36-
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
37-
puzzle_actual_result = 'Unknown'
42+
puzzle_input = test_data[case_to_test]["input"]
43+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
44+
puzzle_actual_result = "Unknown"
3845

3946

4047
# -------------------------------- Actual code execution -------------------------------- #
41-
registers = {'a':0, 'b':0, 'c':0, 'd':0}
48+
registers = {"a": 0, "b": 0, "c": 0, "d": 0}
4249
if part_to_test == 2:
43-
registers['c'] = 1
50+
registers["c"] = 1
4451

4552

46-
instructions = puzzle_input.split('\n')
53+
instructions = [line.split(" ") for line in puzzle_input.split("\n")]
4754
i = 0
4855
while True:
49-
instruction = instructions[i]
56+
ins = instructions[i]
5057
i += 1
5158

52-
if instruction[0:3] == 'cpy':
53-
_, val, target = instruction.split(' ')
59+
if ins[0] == "cpy":
5460
try:
55-
registers[target] = int(val)
61+
registers[ins[2]] = int(ins[1])
5662
except ValueError:
57-
registers[target] = registers[val]
58-
59-
elif instruction[0:3] == 'inc':
60-
_, target = instruction.split(' ')
61-
registers[target] += 1
62-
elif instruction[0:3] == 'dec':
63-
_, target = instruction.split(' ')
64-
registers[target] -= 1
65-
66-
elif instruction[0:3] == 'jnz':
67-
_, target, jump = instruction.split(' ')
68-
if target == '0':
63+
registers[ins[2]] = registers[ins[1]]
64+
65+
elif ins[0] == "inc":
66+
registers[ins[1]] += 1
67+
elif ins[0] == "dec":
68+
registers[ins[1]] -= 1
69+
70+
elif ins[0] == "jnz":
71+
if ins[1] == "0":
6972
pass
7073
else:
7174
try:
72-
if int(target):
73-
i = i + int(jump) - 1 # -1 to compensate for what we added before
75+
if int(ins[1]):
76+
i += int(ins[2]) - 1 # -1 to compensate for what we added before
7477
except ValueError:
75-
if registers[target] != 0:
76-
i = i + int(jump) - 1 # -1 to compensate for what we added before
78+
if registers[ins[1]] != 0:
79+
i += int(ins[2]) - 1 # -1 to compensate for what we added before
7780

7881
if i >= len(instructions):
7982
break
8083

81-
puzzle_actual_result = registers['a']
82-
83-
84-
84+
puzzle_actual_result = registers["a"]
8585

8686

8787
# -------------------------------- Outputs / results -------------------------------- #
8888

8989
if verbose_level >= 3:
90-
print ('Input : ' + puzzle_input)
91-
print ('Expected result : ' + str(puzzle_expected_result))
92-
print ('Actual result : ' + str(puzzle_actual_result))
93-
94-
95-
96-
90+
print("Input : " + puzzle_input)
91+
print("Expected result : " + str(puzzle_expected_result))
92+
print("Actual result : " + str(puzzle_actual_result))

2016/12-Leonardo's Monorail.v1.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {
8+
"input": """cpy 41 a
9+
inc a
10+
inc a
11+
dec a
12+
jnz a 2
13+
dec a""",
14+
"expected": ["42", "Unknown"],
15+
}
16+
17+
test += 1
18+
test_data[test] = {
19+
"input": """""",
20+
"expected": ["Unknown", "Unknown"],
21+
}
22+
23+
test = "real"
24+
input_file = os.path.join(
25+
os.path.dirname(__file__),
26+
"Inputs",
27+
os.path.basename(__file__).replace(".py", ".txt"),
28+
)
29+
test_data[test] = {
30+
"input": open(input_file, "r+").read().strip(),
31+
"expected": ["318083", "9227737"],
32+
}
33+
34+
# -------------------------------- Control program execution -------------------------------- #
35+
36+
case_to_test = "real"
37+
part_to_test = 2
38+
verbose_level = 1
39+
40+
# -------------------------------- Initialize some variables -------------------------------- #
41+
42+
puzzle_input = test_data[case_to_test]["input"]
43+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
44+
puzzle_actual_result = "Unknown"
45+
46+
47+
# -------------------------------- Actual code execution -------------------------------- #
48+
registers = {"a": 0, "b": 0, "c": 0, "d": 0}
49+
if part_to_test == 2:
50+
registers["c"] = 1
51+
52+
53+
instructions = puzzle_input.split("\n")
54+
i = 0
55+
while True:
56+
instruction = instructions[i]
57+
i += 1
58+
59+
if instruction[0:3] == "cpy":
60+
_, val, target = instruction.split(" ")
61+
try:
62+
registers[target] = int(val)
63+
except ValueError:
64+
registers[target] = registers[val]
65+
66+
elif instruction[0:3] == "inc":
67+
_, target = instruction.split(" ")
68+
registers[target] += 1
69+
elif instruction[0:3] == "dec":
70+
_, target = instruction.split(" ")
71+
registers[target] -= 1
72+
73+
elif instruction[0:3] == "jnz":
74+
_, target, jump = instruction.split(" ")
75+
if target == "0":
76+
pass
77+
else:
78+
try:
79+
if int(target):
80+
i = i + int(jump) - 1 # -1 to compensate for what we added before
81+
except ValueError:
82+
if registers[target] != 0:
83+
i = i + int(jump) - 1 # -1 to compensate for what we added before
84+
85+
if i >= len(instructions):
86+
break
87+
88+
puzzle_actual_result = registers["a"]
89+
90+
91+
# -------------------------------- Outputs / results -------------------------------- #
92+
93+
if verbose_level >= 3:
94+
print("Input : " + puzzle_input)
95+
print("Expected result : " + str(puzzle_expected_result))
96+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/Piratmac/adventofcode-python/commit/d737eb6de1973d2cbeefa4ff44762167a3a02fce

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy