Skip to content

Commit 4c952ed

Browse files
committed
Added days 2016-13 to 2016-18 and modified pathfinding library
1 parent 54cdb62 commit 4c952ed

7 files changed

+522
-65
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, pathfinding
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": (10, (7, 4)),
8+
"expected": ['11', '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": (1352, (31, 39)),
18+
"expected": ['90', '135'],
19+
}
20+
21+
# -------------------------------- Control program execution -------------------------------- #
22+
23+
case_to_test = 'real'
24+
part_to_test = 2
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+
# Grid generation
37+
grid = ''
38+
39+
for y in range (50):
40+
for x in range (50):
41+
val = x*x + 3*x + 2*x*y + y + y*y + puzzle_input[0]
42+
bits = "{0:b}".format(val).count('1')
43+
if bits % 2 == 0:
44+
grid += '.'
45+
else:
46+
grid += '#'
47+
grid += '\n'
48+
49+
graph = pathfinding.WeightedGraph()
50+
graph.grid_to_vertices(grid)
51+
52+
53+
54+
if part_to_test == 1:
55+
graph.a_star_search((1, 1), puzzle_input[1])
56+
puzzle_actual_result = len(graph.path(puzzle_input[1]))-1
57+
58+
else:
59+
graph.dijkstra((1, 1), puzzle_input[1])
60+
puzzle_actual_result = len([x for x in graph.distance_from_start if graph.distance_from_start[x] <= 50])
61+
62+
63+
# -------------------------------- Outputs / results -------------------------------- #
64+
65+
if verbose_level >= 3:
66+
print ('Input : ' + puzzle_input)
67+
print ('Expected result : ' + str(puzzle_expected_result))
68+
print ('Actual result : ' + str(puzzle_actual_result))
69+
70+
71+
72+

2016/14-One-Time Pad.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, hashlib
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """abc""",
8+
"expected": ['22728', '22551'],
9+
}
10+
11+
test += 1
12+
test_data[test] = {"input": """""",
13+
"expected": ['Unknown', 'Unknown'],
14+
}
15+
16+
test = 'real'
17+
test_data[test] = {"input": 'qzyelonm',
18+
"expected": ['15168', '20864'],
19+
}
20+
21+
# -------------------------------- Control program execution -------------------------------- #
22+
23+
case_to_test = 'real'
24+
part_to_test = 2
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+
index = 0
38+
found_keys = 0
39+
while True:
40+
index += 1
41+
init_hash = hashlib.md5((puzzle_input + str(index)).encode('utf-8')).hexdigest()
42+
triplets = [x for x in '0123456789abcdef' if x*3 in init_hash]
43+
44+
if triplets:
45+
first_triplet_position = min ([init_hash.find(x*3) for x in triplets])
46+
triplet = init_hash[first_triplet_position]
47+
48+
for i in range (1, 1000):
49+
new_hash = hashlib.md5((puzzle_input + str(index + i)).encode('utf-8')).hexdigest()
50+
if triplet * 5 in new_hash:
51+
found_keys += 1
52+
print (init_hash, triplet, index, index + i, found_keys)
53+
break
54+
55+
if found_keys == 64:
56+
puzzle_actual_result = index
57+
break
58+
59+
60+
else:
61+
hashes = []
62+
hashes_first_triplet = {}
63+
hashes_quintuplets = {}
64+
index = -1
65+
found_keys = 0
66+
67+
for i in range (30000):
68+
hash_text = puzzle_input + str(i)
69+
for y in range (2017):
70+
hash_text = hashlib.md5(hash_text.encode('utf-8')).hexdigest()
71+
hashes.append(hash_text)
72+
73+
triplets = [x for x in '0123456789abcdef' if x*3 in hash_text]
74+
75+
if triplets:
76+
first_triplet_position = min ([hash_text.find(x*3) for x in triplets])
77+
hashes_first_triplet[i] = hash_text[first_triplet_position]
78+
79+
quintuplets = [x for x in '0123456789abcdef' if x*5 in hash_text]
80+
81+
if quintuplets:
82+
hashes_quintuplets[i] = quintuplets
83+
84+
if i % 100 == 0:
85+
print ('calculated', i)
86+
87+
print ('Calculated hashes')
88+
89+
90+
print (hashes_first_triplet)
91+
print (hashes_quintuplets)
92+
93+
for index, triplet in hashes_first_triplet.items():
94+
for i in range (1, 1000):
95+
if index + i in hashes_quintuplets:
96+
if triplet in hashes_quintuplets[index + i]:
97+
found_keys += 1
98+
print (hashes[index], triplet, index, index + i, found_keys)
99+
break
100+
101+
if found_keys == 64:
102+
puzzle_actual_result = index
103+
break
104+
105+
106+
107+
# -------------------------------- Outputs / results -------------------------------- #
108+
109+
if verbose_level >= 3:
110+
print ('Input : ' + puzzle_input)
111+
print ('Expected result : ' + str(puzzle_expected_result))
112+
print ('Actual result : ' + str(puzzle_actual_result))
113+
114+
115+
116+

2016/15-Timing is Everything.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """Disc #1 has 5 positions; at time=0, it is at position 4.
8+
Disc #2 has 2 positions; at time=0, it is at position 1.""",
9+
"expected": ['5', 'Unknown'],
10+
}
11+
12+
test += 1
13+
test_data[test] = {"input": """""",
14+
"expected": ['Unknown', 'Unknown'],
15+
}
16+
17+
test = 'real'
18+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
19+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
20+
"expected": ['16824', '3543984'],
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+
disks = []
39+
for string in puzzle_input.split('\n'):
40+
_, disk, _, total_pos, _, _, _, _, _, _, _, start_position = string.split(' ')
41+
disks.append((int(disk[1:]), int(total_pos), int(start_position[:-1])))
42+
43+
if part_to_test == 2:
44+
disks.append((len(disks)+1, 11, 0))
45+
46+
print (disks)
47+
48+
time = 0
49+
while True:
50+
disk_ok = 0
51+
for disk in disks:
52+
disk_nr, total, start = disk
53+
54+
if (time + disk_nr + start) % total == 0:
55+
56+
disk_ok += 1
57+
58+
if disk_ok == len(disks):
59+
puzzle_actual_result = time
60+
break
61+
62+
time += 1
63+
64+
65+
66+
67+
# -------------------------------- Outputs / results -------------------------------- #
68+
69+
if verbose_level >= 3:
70+
print ('Input : ' + puzzle_input)
71+
print ('Expected result : ' + str(puzzle_expected_result))
72+
print ('Actual result : ' + str(puzzle_actual_result))
73+
74+
75+
76+

2016/16-Dragon Checksum.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": ('110010110100', 12),
8+
"expected": '100',
9+
}
10+
11+
test += 1
12+
test_data[test] = {"input": ('10000', 20),
13+
"expected": '01100',
14+
}
15+
16+
test = 'real'
17+
test_data[test] = {"input": ('10001110011110000', 272),
18+
"expected": '10010101010011101',
19+
}
20+
test = 'real2'
21+
test_data[test] = {"input": ('10001110011110000', 35651584),
22+
"expected": '01100111101101111',
23+
}
24+
25+
# -------------------------------- Control program execution -------------------------------- #
26+
27+
case_to_test = 'real2'
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']
34+
puzzle_actual_result = 'Unknown'
35+
36+
37+
# -------------------------------- Actual code execution -------------------------------- #
38+
39+
disk_contents = puzzle_input[0]
40+
while len(disk_contents) < puzzle_input[1]:
41+
disk_contents += '0' + disk_contents[::-1].replace('1', 'a').replace('0', '1').replace('a', '0')
42+
43+
disk_contents = disk_contents[:puzzle_input[1]]
44+
45+
46+
47+
new_checksum = disk_contents
48+
while len(new_checksum) % 2 == 0:
49+
old_checksum = new_checksum
50+
new_checksum = ''
51+
52+
for i in range (len(old_checksum) // 2):
53+
54+
if old_checksum[i*2] == old_checksum[i*2+1]:
55+
new_checksum += '1'
56+
else:
57+
new_checksum += '0'
58+
59+
puzzle_actual_result = new_checksum
60+
61+
62+
63+
# -------------------------------- Outputs / results -------------------------------- #
64+
65+
if verbose_level >= 3:
66+
print ('Input : ' + puzzle_input)
67+
print ('Expected result : ' + str(puzzle_expected_result))
68+
print ('Actual result : ' + str(puzzle_actual_result))
69+
70+
71+
72+

0 commit comments

Comments
 (0)