Skip to content

Commit d1afc54

Browse files
committed
Added days 2016-21 to 2016-25, plus a few fixes
1 parent d68ad2b commit d1afc54

7 files changed

+630
-2
lines changed

2016/17-Two Steps Forward.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def neighbors (self, vertex):
7272

7373
vault = pathfinding.Graph([((0, 0), '')])
7474

75-
vault.breath_first_search(((0, 0), ''))
75+
vault.breadth_first_search(((0, 0), ''))
7676

7777
if part_to_test == 1:
7878
for vertex in vault.vertices:

2016/21-Scrambled Letters and Hash.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, itertools
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": ('abcde', """swap position 4 with position 0
8+
swap letter d with letter b
9+
reverse positions 0 through 4
10+
rotate left 1 step
11+
move position 1 to position 4
12+
move position 3 to position 0
13+
rotate based on position of letter b
14+
rotate based on position of letter d"""),
15+
"expected": ['Unknown', 'Unknown'],
16+
}
17+
18+
test += 1
19+
test_data[test] = {"input": """""",
20+
"expected": ['Unknown', '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": ('abcdefgh', open(input_file, "r+").read().strip()),
26+
"expected": ['bgfacdeh', 'bdgheacf'],
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+
45+
def scramble_password (puzzle_input):
46+
password = puzzle_input[0]
47+
for string in puzzle_input[1].split('\n'):
48+
if string[0:13] == 'swap position':
49+
_, _, x, _, _, y = string.split(' ')
50+
x, y = int(x), int(y)
51+
x, y = min(x, y), max(x, y)
52+
new_password = password[0:x] + password[y] + password[x+1:y] + password[x] + password[y+1:]
53+
elif string[0:11] == 'swap letter':
54+
_, _, x, _, _, y = string.split(' ')
55+
new_password = password.replace(x, '#').replace(y, x).replace('#', y)
56+
elif string[0:12] == 'rotate based':
57+
_, _, _, _, _, _, letter = string.split(' ')
58+
position = password.find(letter)
59+
if position >= 4:
60+
position += 2
61+
else:
62+
position += 1
63+
new_password = password[-position%len(password):] + password[0:-position%len(password)]
64+
elif string[0:17] == 'reverse positions':
65+
_, _, x, _, y = string.split(' ')
66+
x, y = int(x), int(y)
67+
new_password = password[0:x] + password[y:x:-1] + password[x] + password[y+1:]
68+
elif string[0:13] == 'move position':
69+
_, _, x, _, _, y = string.split(' ')
70+
x, y = int(x), int(y)
71+
if x < y:
72+
new_password = password[0:x] + password[x+1:y+1] + password[x] + password[y+1:]
73+
else:
74+
new_password = password[0:y] + password[x] + password[y:x] + password[x+1:]
75+
else:
76+
_, direction, x, _ = string.split(' ')
77+
x = int(x)
78+
if direction == 'left':
79+
new_password = password[x:] + password[0:x]
80+
else:
81+
new_password = password[len(password)-x:] + password[0:len(password)-x]
82+
password = new_password
83+
# print (string, password)
84+
return password
85+
86+
if part_to_test == 1:
87+
puzzle_actual_result = scramble_password(puzzle_input)
88+
89+
90+
else:
91+
for combination in itertools.permutations('abcdefgh'):
92+
password = ''.join(combination)
93+
scrambled = scramble_password((password, puzzle_input[1]))
94+
print (password, scrambled)
95+
if scrambled == 'fbgdceah':
96+
puzzle_actual_result = password
97+
break
98+
99+
100+
101+
102+
# -------------------------------- Outputs / results -------------------------------- #
103+
104+
if verbose_level >= 3:
105+
print ('Input : ' + puzzle_input)
106+
print ('Expected result : ' + str(puzzle_expected_result))
107+
print ('Actual result : ' + str(puzzle_actual_result))
108+
109+
110+
111+

2016/22-Grid Computing.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """""",
8+
"expected": ['Unknown', 'Unknown'],
9+
}
10+
11+
test += 1
12+
test_data[test] = {"input": """""",
13+
"expected": ['Unknown', 'Unknown'],
14+
}
15+
16+
test = 'real'
17+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
18+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
19+
"expected": ['946', '195'],
20+
}
21+
22+
# -------------------------------- Control program execution -------------------------------- #
23+
24+
case_to_test = 'real'
25+
part_to_test = 2
26+
verbose_level = 1
27+
28+
# -------------------------------- Initialize some variables -------------------------------- #
29+
30+
puzzle_input = test_data[case_to_test]['input']
31+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
32+
puzzle_actual_result = 'Unknown'
33+
34+
35+
# -------------------------------- Actual code execution -------------------------------- #
36+
37+
if part_to_test == 1:
38+
nodes_used = {}
39+
nodes_avail = {}
40+
for string in puzzle_input.split('\n'):
41+
if string == '':
42+
continue
43+
if string[0] != '/':
44+
continue
45+
46+
string = string.replace(' ', ' ').replace(' ', ' ').replace(' ', ' ')
47+
name, size, used, avail, use = string.split (' ')
48+
used, avail = int(used[:-1]), int(avail[:-1])
49+
nodes_used[name] = used
50+
nodes_avail[name] = avail
51+
52+
count_pairs = 0
53+
for node, used in nodes_used.items():
54+
if used == 0:
55+
continue
56+
count_pairs += len([name for name, avail in nodes_avail.items() if name != node and used <= avail])
57+
58+
puzzle_actual_result = count_pairs
59+
60+
61+
62+
63+
else:
64+
# All small nodes can contain the data of all other small nodes
65+
# Very large nodes can't contain more data, and can't transfer their data
66+
# So, basically, they are walls
67+
# There is one empty node
68+
# That node is basically the "player" which moves around
69+
# It takes 45 moves for that player to move left to the goal, and for the goal to move to that player
70+
# Then, it takes 5 moves for the player to go around the goal, then for the goal to move to the player
71+
# So, every movement to the left takes 5 moves
72+
# There are 30 leftbound moved to make (first one is included in the 45):
73+
puzzle_actual_result = 45 + 5*30
74+
75+
76+
77+
78+
# -------------------------------- Outputs / results -------------------------------- #
79+
80+
if verbose_level >= 3:
81+
print ('Input : ' + puzzle_input)
82+
print ('Expected result : ' + str(puzzle_expected_result))
83+
print ('Actual result : ' + str(puzzle_actual_result))
84+
85+
86+
87+

2016/23-Safe Cracking.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """cpy 2 a
8+
tgl a
9+
tgl a
10+
tgl a
11+
cpy 1 a
12+
dec a
13+
dec a""",
14+
"expected": ['3', 'Unknown'],
15+
}
16+
17+
test += 1
18+
test_data[test] = {"input": """""",
19+
"expected": ['Unknown', 'Unknown'],
20+
}
21+
22+
test = 'real'
23+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
24+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
25+
"expected": ['10886', '479007446'],
26+
}
27+
28+
# -------------------------------- Control program execution -------------------------------- #
29+
30+
case_to_test = 'real'
31+
part_to_test = 1
32+
verbose_level = 1
33+
34+
35+
def RepresentsInt(s):
36+
try:
37+
int(s)
38+
return True
39+
except ValueError:
40+
return False
41+
42+
# -------------------------------- Initialize some variables -------------------------------- #
43+
44+
puzzle_input = test_data[case_to_test]['input']
45+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
46+
puzzle_actual_result = 'Unknown'
47+
48+
49+
# -------------------------------- Actual code execution -------------------------------- #
50+
registers = {'a':0, 'b':0, 'c':0, 'd':0}
51+
52+
instructions = puzzle_input.split('\n')
53+
i = 0
54+
if case_to_test == 'real':
55+
if part_to_test == 1:
56+
registers['a'] = 7
57+
else:
58+
registers['a'] = 12
59+
60+
while True:
61+
instruction = instructions[i]
62+
i += 1
63+
64+
# print (i, instruction, registers)
65+
66+
if instruction[0:3] == 'cpy':
67+
_, val, target = instruction.split(' ')
68+
try:
69+
registers[target] = int(val)
70+
except ValueError:
71+
registers[target] = registers[val]
72+
73+
elif instruction[0:3] == 'inc':
74+
_, target = instruction.split(' ')
75+
if target in registers:
76+
registers[target] += 1
77+
elif instruction[0:3] == 'dec':
78+
_, target = instruction.split(' ')
79+
if target in registers:
80+
registers[target] -= 1
81+
82+
elif instruction[0:3] == 'tgl':
83+
_, target = instruction.split(' ')
84+
target = registers[target]
85+
86+
target_position = i+target-1 # -1 because we added 1 to i before
87+
if target_position < len(instructions):
88+
89+
if instructions[target_position][0:3] == 'inc':
90+
instructions[target_position] = 'dec' + instructions[target_position][3:]
91+
elif instructions[target_position][0:3] == 'dec':
92+
instructions[target_position] = 'inc' + instructions[target_position][3:]
93+
elif instructions[target_position][0:3] == 'jnz':
94+
instructions[target_position] = 'cpy' + instructions[target_position][3:]
95+
elif instructions[target_position][0:3] == 'cpy':
96+
instructions[target_position] = 'jnz' + instructions[target_position][3:]
97+
elif instructions[target_position][0:3] == 'tgl':
98+
instructions[target_position] = 'inc' + instructions[target_position][3:]
99+
100+
elif instruction[0:3] == 'jnz':
101+
_, target, jump = instruction.split(' ')
102+
if target == '0':
103+
pass
104+
else:
105+
if RepresentsInt(target) and RepresentsInt(jump):
106+
i = i + int(jump) - 1 # -1 to compensate for what we added before
107+
elif RepresentsInt(target):
108+
i = i + registers[jump] - 1 # -1 to compensate for what we added before
109+
elif RepresentsInt(jump):
110+
if registers[target] != 0:
111+
i = i + int(jump) - 1 # -1 to compensate for what we added before
112+
113+
elif instruction[0:3] == 'add':
114+
_, source, target = instruction.split(' ')
115+
if source == '0':
116+
pass
117+
else:
118+
if RepresentsInt(source):
119+
registers[target] += int(source)
120+
else:
121+
registers[target] += registers[source]
122+
123+
elif instruction[0:3] == 'mul':
124+
_, source, target = instruction.split(' ')
125+
if source == '0':
126+
pass
127+
else:
128+
if RepresentsInt(source):
129+
registers[target] *= int(source)
130+
else:
131+
registers[target] *= registers[source]
132+
133+
if i >= len(instructions):
134+
break
135+
136+
puzzle_actual_result = registers['a']
137+
138+
139+
140+
141+
142+
# -------------------------------- Outputs / results -------------------------------- #
143+
144+
if verbose_level >= 3:
145+
print ('Input : ' + puzzle_input)
146+
print ('Expected result : ' + str(puzzle_expected_result))
147+
print ('Actual result : ' + str(puzzle_actual_result))
148+
149+
150+
151+

0 commit comments

Comments
 (0)