Skip to content

Commit 72b7d89

Browse files
committed
Added days 2017-05 to 2017-08
1 parent 6b93886 commit 72b7d89

File tree

4 files changed

+374
-0
lines changed

4 files changed

+374
-0
lines changed
Lines changed: 92 additions & 0 deletions
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
8+
3
9+
0
10+
1
11+
-3""",
12+
"expected": ['Unknown', 'Unknown'],
13+
}
14+
15+
test += 1
16+
test_data[test] = {"input": """""",
17+
"expected": ['Unknown', 'Unknown'],
18+
}
19+
20+
test = 'real'
21+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
22+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
23+
"expected": ['339351', '24315397'],
24+
}
25+
26+
# -------------------------------- Control program execution -------------------------------- #
27+
28+
case_to_test = 'real'
29+
part_to_test = 2
30+
verbose_level = 1
31+
32+
# -------------------------------- Initialize some variables -------------------------------- #
33+
34+
puzzle_input = test_data[case_to_test]['input']
35+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
36+
puzzle_actual_result = 'Unknown'
37+
38+
39+
# -------------------------------- Actual code execution -------------------------------- #
40+
41+
if part_to_test == 1:
42+
instructions = list(map(int, puzzle_input.split('\n')))
43+
i = 0
44+
step = 0
45+
while True:
46+
try:
47+
instruction = instructions[i]
48+
instructions[i] += 1
49+
except IndexError:
50+
break
51+
52+
step += 1
53+
54+
i = i + instruction
55+
56+
puzzle_actual_result = step
57+
58+
59+
60+
61+
else:
62+
instructions = list(map(int, puzzle_input.split('\n')))
63+
i = 0
64+
step = 0
65+
while True:
66+
try:
67+
instruction = instructions[i]
68+
if instructions[i] >= 3:
69+
instructions[i] -= 1
70+
else:
71+
instructions[i] += 1
72+
except IndexError:
73+
break
74+
75+
step += 1
76+
77+
i = i + instruction
78+
79+
puzzle_actual_result = step
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/06-Memory Reallocation.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """0 2 7 0""",
8+
"expected": ['5', 'Unknown'],
9+
}
10+
11+
test += 1
12+
test_data[test] = {"input": """""",
13+
"expected": ['Unknown', 'Unknown'],
14+
}
15+
16+
test = 'real'
17+
test_data[test] = {"input": '14 0 15 12 11 11 3 5 1 6 8 4 9 1 8 4',
18+
"expected": ['11137', '1037'],
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+
banks_history = [list(map(int, puzzle_input.split(' ')))]
37+
steps = 0
38+
while True:
39+
banks = banks_history[steps].copy()
40+
bank_id = min([x for x in range(len(banks)) if banks[x] == max(banks)])
41+
redistribute = banks[bank_id]
42+
banks[bank_id] = 0
43+
for i in range(1, redistribute + 1):
44+
banks[(bank_id + i) % len(banks)] += 1
45+
46+
steps += 1
47+
if banks in banks_history:
48+
if part_to_test == 1:
49+
puzzle_actual_result = steps
50+
else:
51+
puzzle_actual_result = steps - min([x for x in range(len(banks_history)) if banks_history[x] == banks])
52+
break
53+
54+
banks_history.append(banks)
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))
63+
64+
65+
66+

2017/07-Recursive Circus.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """pbga (66)
8+
xhth (57)
9+
ebii (61)
10+
havc (66)
11+
ktlj (57)
12+
fwft (72) -> ktlj, cntj, xhth
13+
qoyq (66)
14+
padx (45) -> pbga, havc, qoyq
15+
tknk (41) -> ugml, padx, fwft
16+
jptl (61)
17+
ugml (68) -> gyxo, ebii, jptl
18+
gyxo (61)
19+
cntj (57)""",
20+
"expected": ['Unknown', 'Unknown'],
21+
}
22+
23+
test += 1
24+
test_data[test] = {"input": """""",
25+
"expected": ['Unknown', 'Unknown'],
26+
}
27+
28+
test = 'real'
29+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
30+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
31+
"expected": ['vtzay', '910'],
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+
49+
all_held = []
50+
holders = []
51+
individual_weights = {}
52+
branches = {}
53+
for string in puzzle_input.split('\n'):
54+
if string == '':
55+
continue
56+
57+
individual_weights[string.split(' ')[0]] = int(string.split(' ')[1][1:-1])
58+
59+
is_holder = string.split('->')
60+
if len(is_holder) == 1:
61+
all_held.append(string.split(' ')[0])
62+
continue
63+
64+
holder, weight, _, *held = string.split(' ')
65+
all_held += [x.replace(',', '') for x in held]
66+
holders.append(holder)
67+
68+
branches[holder] = [x.replace(',', '') for x in held]
69+
70+
for holder in holders:
71+
if holder not in all_held:
72+
puzzle_actual_result = holder
73+
break
74+
75+
76+
if part_to_test == 2:
77+
unknown_weights = holders.copy()
78+
held_weight = {}
79+
total_weight = {x:individual_weights[x] for x in individual_weights if x not in holders}
80+
mismatch = {}
81+
while len(unknown_weights):
82+
for holder in unknown_weights:
83+
if all([x in total_weight for x in branches[holder]]):
84+
# We know the weights of all leaves, including sub-towers
85+
86+
held_weight[holder] = [total_weight[x] for x in branches[holder]]
87+
if any([x != held_weight[holder][0] for x in held_weight[holder]]):
88+
mismatch.update({holder: held_weight[holder]})
89+
total_weight[holder] = sum(held_weight[holder]) + individual_weights[holder]
90+
unknown_weights.remove(holder)
91+
92+
# This is very ugly code
93+
# First, determine which mismatch disk has the minimum weight (because that's the closest to the problem)
94+
min_weight = min([y for x in mismatch for y in mismatch[x]])
95+
min_holder = [x for x in mismatch if min_weight in mismatch[x]][0]
96+
97+
# Then, determine what are the correct and incorrect weights
98+
count_weights = {mismatch[min_holder].count(x):x for x in mismatch[min_holder]}
99+
wrong_weight = count_weights[1]
100+
correct_weight = count_weights[len(mismatch[min_holder])-1]
101+
delta = correct_weight - wrong_weight
102+
103+
# Find which tower has the wrong individual weight, then calculate its new weight
104+
wrong_holder = [x for x in branches[min_holder] if total_weight[x] == wrong_weight][0]
105+
new_weight = individual_weights[wrong_holder] + delta
106+
107+
puzzle_actual_result = new_weight
108+
109+
110+
# -------------------------------- Outputs / results -------------------------------- #
111+
112+
if verbose_level >= 3:
113+
print ('Input : ' + puzzle_input)
114+
print ('Expected result : ' + str(puzzle_expected_result))
115+
print ('Actual result : ' + str(puzzle_actual_result))
116+
117+
118+
119+
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """b inc 5 if a > 1
8+
a inc 1 if b < 5
9+
c dec -10 if a >= 1
10+
c inc -20 if c == 10""",
11+
"expected": ['Unknown', 'Unknown'],
12+
}
13+
14+
test += 1
15+
test_data[test] = {"input": """""",
16+
"expected": ['Unknown', 'Unknown'],
17+
}
18+
19+
test = 'real'
20+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
21+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
22+
"expected": ['4416', '5199'],
23+
}
24+
25+
# -------------------------------- Control program execution -------------------------------- #
26+
27+
case_to_test = 'real'
28+
part_to_test = 2
29+
verbose_level = 1
30+
31+
# -------------------------------- Initialize some variables -------------------------------- #
32+
33+
puzzle_input = test_data[case_to_test]['input']
34+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
35+
puzzle_actual_result = 'Unknown'
36+
37+
def is_integer (value):
38+
try:
39+
val = int(value)
40+
return True
41+
except ValueError:
42+
return False
43+
44+
def apply_operation (val1, action, val2):
45+
if action == 'dec':
46+
return val1 - int(val2)
47+
else:
48+
return val1 + int(val2)
49+
50+
# -------------------------------- Actual code execution -------------------------------- #
51+
registers = {}
52+
max_value = 0
53+
for string in puzzle_input.split('\n'):
54+
target, action, value, _, source, condition, operand = string.split(' ')
55+
if not target in registers:
56+
registers[target] = 0
57+
if not source in registers:
58+
registers[source] = 0
59+
60+
if condition == '==':
61+
if registers[source] == int(operand):
62+
registers[target] = apply_operation (registers[target], action, value)
63+
elif condition == '!=':
64+
if registers[source] != int(operand):
65+
registers[target] = apply_operation (registers[target], action, value)
66+
elif condition == '>=':
67+
if registers[source] >= int(operand):
68+
registers[target] = apply_operation (registers[target], action, value)
69+
elif condition == '<=':
70+
if registers[source] <= int(operand):
71+
registers[target] = apply_operation (registers[target], action, value)
72+
elif condition == '>':
73+
if registers[source] > int(operand):
74+
registers[target] = apply_operation (registers[target], action, value)
75+
elif condition == '<':
76+
if registers[source] < int(operand):
77+
registers[target] = apply_operation (registers[target], action, value)
78+
79+
max_value = max(max_value, max(registers.values()))
80+
81+
if part_to_test == 1:
82+
puzzle_actual_result = max(registers.values())
83+
else:
84+
puzzle_actual_result = max_value
85+
86+
87+
88+
# -------------------------------- Outputs / results -------------------------------- #
89+
90+
if verbose_level >= 3:
91+
print ('Input : ' + puzzle_input)
92+
print ('Expected result : ' + str(puzzle_expected_result))
93+
print ('Actual result : ' + str(puzzle_actual_result))
94+
95+
96+
97+

0 commit comments

Comments
 (0)