Skip to content

Commit 0b9e5d0

Browse files
committed
Improved performance for day 2018-01
1 parent 34dbe35 commit 0b9e5d0

2 files changed

Lines changed: 94 additions & 31 deletions

File tree

2018/01-Chronal Calibration.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@
44
test_data = {}
55

66
test = 1
7-
test_data[test] = {"input": """""",
8-
"expected": ['Unknown', 'Unknown'],
9-
}
10-
11-
test = 'real'
12-
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
13-
test_data[test] = {"input": open(input_file, "r+").read().strip(),
14-
"expected": ['585', '83173'],
15-
}
7+
test_data[test] = {
8+
"input": """""",
9+
"expected": ["Unknown", "Unknown"],
10+
}
11+
12+
test = "real"
13+
input_file = os.path.join(
14+
os.path.dirname(__file__),
15+
"Inputs",
16+
os.path.basename(__file__).replace(".py", ".txt"),
17+
)
18+
test_data[test] = {
19+
"input": open(input_file, "r+").read().strip(),
20+
"expected": ["585", "83173"],
21+
}
1622

1723
# -------------------------------- Control program execution -------------------------------- #
1824

19-
case_to_test = 'real'
20-
part_to_test = 2
25+
case_to_test = "real"
26+
part_to_test = 2
2127
verbose_level = 1
2228

2329
# -------------------------------- Initialize some variables -------------------------------- #
2430

25-
puzzle_input = test_data[case_to_test]['input']
26-
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
27-
puzzle_actual_result = 'Unknown'
31+
puzzle_input = test_data[case_to_test]["input"]
32+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
33+
puzzle_actual_result = "Unknown"
2834

2935

3036
# -------------------------------- Actual code execution -------------------------------- #
@@ -34,28 +40,23 @@
3440

3541

3642
else:
37-
used_frequencies = [0]
43+
data = list(map(int, puzzle_input.splitlines()))
44+
used_frequencies = [sum(data[0 : i + 1]) for i in range(len(data))]
45+
delta = sum(map(int, puzzle_input.splitlines()))
3846
frequency = 0
47+
i = 0
3948
while True:
40-
for string in puzzle_input.split('\n'):
41-
frequency += int(string)
42-
if frequency in used_frequencies:
43-
puzzle_actual_result = frequency
44-
break
45-
used_frequencies.append(frequency)
46-
47-
if puzzle_actual_result != 'Unknown':
49+
i += 1
50+
new_freq = [x + i * delta for x in used_frequencies]
51+
reuse = [freq for freq in new_freq if freq in used_frequencies]
52+
if reuse:
53+
puzzle_actual_result = reuse[0]
4854
break
4955

5056

51-
5257
# -------------------------------- Outputs / results -------------------------------- #
5358

5459
if verbose_level >= 3:
55-
print ('Input : ' + puzzle_input)
56-
print ('Expected result : ' + str(puzzle_expected_result))
57-
print ('Actual result : ' + str(puzzle_actual_result))
58-
59-
60-
61-
60+
print("Input : " + puzzle_input)
61+
print("Expected result : " + str(puzzle_expected_result))
62+
print("Actual result : " + str(puzzle_actual_result))

2018/01-Chronal Calibration.v1.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {
8+
"input": """""",
9+
"expected": ["Unknown", "Unknown"],
10+
}
11+
12+
test = "real"
13+
input_file = os.path.join(
14+
os.path.dirname(__file__),
15+
"Inputs",
16+
os.path.basename(__file__).replace(".py", ".txt"),
17+
)
18+
test_data[test] = {
19+
"input": open(input_file, "r+").read().strip(),
20+
"expected": ["585", "83173"],
21+
}
22+
23+
# -------------------------------- Control program execution -------------------------------- #
24+
25+
case_to_test = "real"
26+
part_to_test = 2
27+
verbose_level = 1
28+
29+
# -------------------------------- Initialize some variables -------------------------------- #
30+
31+
puzzle_input = test_data[case_to_test]["input"]
32+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
33+
puzzle_actual_result = "Unknown"
34+
35+
36+
# -------------------------------- Actual code execution -------------------------------- #
37+
38+
if part_to_test == 1:
39+
puzzle_actual_result = sum(map(int, puzzle_input.splitlines()))
40+
41+
42+
else:
43+
used_frequencies = [0]
44+
frequency = 0
45+
while True:
46+
for string in puzzle_input.split("\n"):
47+
frequency += int(string)
48+
if frequency in used_frequencies:
49+
puzzle_actual_result = frequency
50+
break
51+
used_frequencies.append(frequency)
52+
53+
if puzzle_actual_result != "Unknown":
54+
break
55+
56+
57+
# -------------------------------- Outputs / results -------------------------------- #
58+
59+
if verbose_level >= 3:
60+
print("Input : " + puzzle_input)
61+
print("Expected result : " + str(puzzle_expected_result))
62+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)