Skip to content

Commit f99302d

Browse files
committed
Added 2017-23, 2017-24 and 2017-25
1 parent 11ca74b commit f99302d

3 files changed

+304
-0
lines changed

2017/23-Coprocessor Conflagration.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, math
3+
4+
test_data = {}
5+
6+
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": ['6724', '903'],
15+
}
16+
17+
# -------------------------------- Control program execution -------------------------------- #
18+
19+
case_to_test = 'real'
20+
part_to_test = 2
21+
verbose_level = 1
22+
23+
# -------------------------------- Initialize some variables -------------------------------- #
24+
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'
28+
29+
30+
# -------------------------------- Actual code execution -------------------------------- #
31+
32+
def val_get (registers, value):
33+
try:
34+
return int(value)
35+
except ValueError:
36+
return registers[value]
37+
38+
def get_divisors (value):
39+
small_divisors = [d for d in range (1, int(math.sqrt(value))+1) if value % d == 0 ]
40+
big_divisors = [value // d for d in small_divisors if not d**2 == value]
41+
return set(small_divisors + big_divisors)
42+
43+
44+
45+
instructions = [(string.split(' ')) for string in puzzle_input.split('\n')]
46+
47+
i = 0
48+
registers = {x:0 for x in 'abcdefgh'}
49+
registers['a'] = part_to_test - 1
50+
count_mul = 0
51+
val_h = 1
52+
nb_instructions = 0
53+
54+
if part_to_test == 1:
55+
while i < len(instructions):
56+
instr = instructions[i]
57+
58+
if instr[0] == 'set':
59+
registers.update({instr[1]: val_get(registers, instr[2])})
60+
elif instr[0] == 'sub':
61+
registers.setdefault(instr[1], 0)
62+
registers[instr[1]] -= val_get(registers, instr[2])
63+
elif instr[0] == 'mul':
64+
registers.setdefault(instr[1], 0)
65+
registers[instr[1]] *= val_get(registers, instr[2])
66+
count_mul += 1
67+
elif instr[0] == 'mod':
68+
registers.setdefault(instr[1], 0)
69+
registers[instr[1]] %= val_get(registers, instr[2])
70+
elif instr[0] == 'jnz':
71+
if val_get(registers, instr[1]) != 0:
72+
i += val_get(registers, instr[2]) - 1
73+
74+
i += 1
75+
nb_instructions += 1
76+
77+
if nb_instructions == 10 ** 7:
78+
break
79+
80+
puzzle_actual_result = count_mul
81+
82+
83+
else:
84+
count_composite = 0
85+
for i in range (84*100+100000, 84*100+100000+17000+1, 17):
86+
if len(get_divisors(i)) != 2:
87+
print (i, get_divisors(i))
88+
count_composite += 1
89+
90+
puzzle_actual_result = count_composite
91+
92+
# 116206 too high
93+
# 500 too low
94+
# 10477 is wrong
95+
96+
# -------------------------------- Outputs / results -------------------------------- #
97+
98+
if verbose_level >= 3:
99+
print ('Input : ' + puzzle_input)
100+
print ('Expected result : ' + str(puzzle_expected_result))
101+
print ('Actual result : ' + str(puzzle_actual_result))
102+
103+
104+
105+

2017/24-Electromagnetic Moat.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """0/2
8+
2/2
9+
2/3
10+
3/4
11+
3/5
12+
0/1
13+
10/1
14+
9/10""",
15+
"expected": ['31', '19'],
16+
}
17+
18+
test = 'real'
19+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
20+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
21+
"expected": ['1940', '1928'],
22+
}
23+
24+
# -------------------------------- Control program execution -------------------------------- #
25+
26+
case_to_test = 'real'
27+
part_to_test = 2
28+
verbose_level = 1
29+
30+
# -------------------------------- Initialize some variables -------------------------------- #
31+
32+
puzzle_input = test_data[case_to_test]['input']
33+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
34+
puzzle_actual_result = 'Unknown'
35+
36+
37+
# -------------------------------- Actual code execution -------------------------------- #
38+
39+
def build_bridge (bridge, last, available_pieces):
40+
global bridges
41+
next_pieces = [x for x in available_pieces if last in x]
42+
43+
for next_piece in next_pieces:
44+
new_bridge = bridge + [next_piece]
45+
new_available_pieces = available_pieces.copy()
46+
new_available_pieces.remove(next_piece)
47+
if next_piece[0] == next_piece[1]:
48+
new_last = next_piece[0]
49+
else:
50+
new_last = [x for x in next_piece if x != last][0]
51+
build_bridge (new_bridge, new_last, new_available_pieces)
52+
53+
bridges.append(bridge)
54+
55+
56+
pieces = []
57+
bridges = []
58+
for string in puzzle_input.split('\n'):
59+
if string == '':
60+
continue
61+
62+
a, b = map(int, string.split('/'))
63+
pieces.append((a, b))
64+
65+
build_bridge([], 0, pieces)
66+
67+
max_strength = 0
68+
if part_to_test == 1:
69+
for bridge in bridges:
70+
max_strength = max (max_strength, sum(map(sum, bridge)))
71+
puzzle_actual_result = max_strength
72+
else:
73+
max_length = max(map(len, bridges))
74+
for bridge in bridges:
75+
if len(bridge) != max_length:
76+
continue
77+
max_strength = max (max_strength, sum(map(sum, bridge)))
78+
puzzle_actual_result = max_strength
79+
80+
81+
82+
83+
# -------------------------------- Outputs / results -------------------------------- #
84+
85+
if verbose_level >= 3:
86+
print ('Input : ' + puzzle_input)
87+
print ('Expected result : ' + str(puzzle_expected_result))
88+
print ('Actual result : ' + str(puzzle_actual_result))
89+
90+
91+
92+

2017/25-The Halting Problem.py

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """Begin in state A.
8+
Perform a diagnostic checksum after 6 steps.
9+
10+
In state A:
11+
If the current value is 0:
12+
- Write the value 1.
13+
- Move one slot to the right.
14+
- Continue with state B.
15+
If the current value is 1:
16+
- Write the value 0.
17+
- Move one slot to the left.
18+
- Continue with state B.
19+
20+
In state B:
21+
If the current value is 0:
22+
- Write the value 1.
23+
- Move one slot to the left.
24+
- Continue with state A.
25+
If the current value is 1:
26+
- Write the value 1.
27+
- Move one slot to the right.
28+
- Continue with state A.""",
29+
"expected": ['3', 'Unknown'],
30+
}
31+
32+
test = 'real'
33+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
34+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
35+
"expected": ['2794', 'Unknown'],
36+
}
37+
38+
# -------------------------------- Control program execution -------------------------------- #
39+
40+
case_to_test = 'real'
41+
part_to_test = 1
42+
verbose_level = 1
43+
44+
# -------------------------------- Initialize some variables -------------------------------- #
45+
46+
puzzle_input = test_data[case_to_test]['input']
47+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
48+
puzzle_actual_result = 'Unknown'
49+
50+
51+
# -------------------------------- Actual code execution -------------------------------- #
52+
53+
if part_to_test == 1:
54+
states = {}
55+
for string in puzzle_input.split('\n'):
56+
if string == '':
57+
continue
58+
59+
if string.startswith('Begin in state '):
60+
start_state = string[-2:-1]
61+
elif string.startswith('Perform a diagnostic checksum after '):
62+
_,_,_,_,_, steps, _ = string.split(' ')
63+
steps = int(steps)
64+
elif string.startswith('In state '):
65+
state = string[-2:-1]
66+
elif string.startswith(' If the current value is'):
67+
current_value = int(string[-2:-1])
68+
elif string.startswith(' - Write the value'):
69+
target_value = int(string[-2:-1])
70+
elif string.startswith(' - Move one slot to the'):
71+
direction = string.split(' ')[-1]
72+
elif string.startswith(' - Continue with state'):
73+
next_state = string[-2:-1]
74+
if state not in states:
75+
states[state] = {}
76+
states[state].update({current_value: (target_value, direction, next_state)})
77+
78+
state = start_state
79+
tape = {0:0}
80+
position = 0
81+
82+
for _ in range (steps):
83+
value = tape[position] if position in tape else 0
84+
tape[position] = states[state][value][0]
85+
position += 1 if states[state][value][1] == 'right.' else -1
86+
state = states[state][value][2]
87+
88+
puzzle_actual_result = sum(tape[x] for x in tape)
89+
90+
91+
92+
else:
93+
pass
94+
95+
96+
97+
98+
# -------------------------------- Outputs / results -------------------------------- #
99+
100+
if verbose_level >= 3:
101+
print ('Input : ' + puzzle_input)
102+
print ('Expected result : ' + str(puzzle_expected_result))
103+
print ('Actual result : ' + str(puzzle_actual_result))
104+
105+
106+
107+

0 commit comments

Comments
 (0)