Skip to content

Commit 5bacbbb

Browse files
committed
Added day 2015-24 and 2015-25
1 parent 3367cc5 commit 5bacbbb

File tree

3 files changed

+158
-3
lines changed

3 files changed

+158
-3
lines changed

2015/23-Opening the Turing Lock.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
test = 'real'
2020
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
2121
test_data[test] = {"input": open(input_file, "r+").read().strip(),
22-
"expected": ['Unknown', 'Unknown'],
22+
"expected": ['170', '247'],
2323
}
2424

2525
# -------------------------------- Control program execution -------------------------------- #
2626

2727
case_to_test = 'real'
28-
part_to_test = 2
28+
part_to_test = 1
2929
verbose_level = 1
3030

3131
# -------------------------------- Initialize some variables -------------------------------- #
@@ -48,7 +48,8 @@
4848
if i >= len(instructions):
4949
break
5050
string = instructions[i]
51-
print (i, string, computer)
51+
if verbose_level >= 2:
52+
print ('Applying instruction', i, ':', string, '- Computer state before : ', computer)
5253
if string == '':
5354
continue
5455
if string[0:3] == 'hlf':

2015/24-It Hangs in the Balance.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, itertools
3+
4+
from operator import mul
5+
from functools import reduce
6+
7+
test_data = {}
8+
9+
test = 1
10+
test_data[test] = {"input": """1
11+
2
12+
3
13+
4
14+
5
15+
7
16+
8
17+
9
18+
10
19+
11""",
20+
"expected": ['99', 'Unknown'],
21+
}
22+
23+
test = 'real'
24+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
25+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
26+
"expected": ['11846773891', 'Unknown'],
27+
}
28+
29+
# -------------------------------- Control program execution -------------------------------- #
30+
31+
case_to_test = 'real'
32+
part_to_test = 2
33+
verbose_level = 1
34+
35+
# -------------------------------- Initialize some variables -------------------------------- #
36+
37+
puzzle_input = test_data[case_to_test]['input']
38+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
39+
puzzle_actual_result = 'Unknown'
40+
41+
42+
# -------------------------------- Actual code execution -------------------------------- #
43+
44+
list_packages = []
45+
46+
mini_quantum_entanglement = 10 ** 100
47+
48+
list_packages = [int(x) for x in puzzle_input.split('\n')]
49+
total_weight = sum(list_packages)
50+
group_weight = total_weight // 3 if part_to_test == 1 else total_weight // 4
51+
52+
for group1_size in range (1, len(list_packages) - 2):
53+
print('Testing with group 1 of size', group1_size)
54+
for group1 in itertools.combinations(list_packages, group1_size):
55+
if sum(group1) != group_weight:
56+
continue
57+
if reduce(mul, group1, 1) >= mini_quantum_entanglement:
58+
continue
59+
60+
remaining_packages = [x for x in list_packages if x not in group1]
61+
62+
for group2_size in range (1, len(remaining_packages) - 2):
63+
print('Testing with group 2 of size', group2_size)
64+
for group2 in itertools.combinations(remaining_packages, group2_size):
65+
if sum(group2) == group_weight:
66+
mini_quantum_entanglement = min(mini_quantum_entanglement, reduce(mul, group1, 1))
67+
68+
if mini_quantum_entanglement != 10 ** 100:
69+
break
70+
71+
puzzle_actual_result = mini_quantum_entanglement
72+
73+
74+
# -------------------------------- Outputs / results -------------------------------- #
75+
76+
if verbose_level >= 3:
77+
print ('Input : ' + puzzle_input)
78+
print ('Expected result : ' + str(puzzle_expected_result))
79+
print ('Actual result : ' + str(puzzle_actual_result))
80+
81+
82+
83+

2015/25-Let It Snow.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": (4, 1),
8+
"expected": ['24592653', 'Unknown'],
9+
}
10+
11+
test += 1
12+
test_data[test] = {"input": (3, 4),
13+
"expected": ['7981243', 'Unknown'],
14+
}
15+
16+
test = 'real'
17+
test_data[test] = {"input": (2981, 3075),
18+
"expected": ['9132360', 'N/A'],
19+
}
20+
21+
# -------------------------------- Control program execution -------------------------------- #
22+
23+
case_to_test = 'real'
24+
part_to_test = 1
25+
verbose_level = 1
26+
27+
# -------------------------------- Initialize some variables -------------------------------- #
28+
29+
puzzle_input = test_data[case_to_test]['input']
30+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
31+
puzzle_actual_result = 'Unknown'
32+
33+
34+
# -------------------------------- Actual code execution -------------------------------- #
35+
36+
if part_to_test == 1:
37+
# First, find what is the index of the code (in other words, we need the n-th code, let's find n)
38+
row, col = puzzle_input
39+
code_index = 1
40+
code_index += row*(row-1) // 2
41+
code_index += col*(col-1) // 2
42+
code_index += row*(col-1)
43+
44+
# Then, calculate it
45+
# The operation we have to do is x * 252533 % 33554393, with x0 = 20151125 and repeat that code_index times
46+
# This translate to 20151125 * 252533^code_index % 33554393
47+
# Modular arythmetic to the rescue!
48+
puzzle_actual_result = 20151125 * pow(252533, code_index-1, 33554393) % 33554393
49+
50+
51+
52+
53+
54+
55+
else:
56+
for string in puzzle_input.split('\n'):
57+
if string == '':
58+
continue
59+
60+
61+
62+
# -------------------------------- Outputs / results -------------------------------- #
63+
64+
if verbose_level >= 3:
65+
print ('Input : ' + puzzle_input)
66+
print ('Expected result : ' + str(puzzle_expected_result))
67+
print ('Actual result : ' + str(puzzle_actual_result))
68+
69+
70+
71+

0 commit comments

Comments
 (0)