Skip to content

Commit d15829e

Browse files
committed
Added day 2017-14
1 parent b22e8e7 commit d15829e

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

2017/14-Disk Defragmentation.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
from functools import reduce
4+
import pathfinding
5+
6+
test_data = {}
7+
8+
test = 1
9+
test_data[test] = {"input": """flqrgnkx""",
10+
"expected": ['8108', '1242'],
11+
}
12+
13+
test = 'real'
14+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
15+
test_data[test] = {"input": 'wenycdww',
16+
"expected": ['8226', '1128'],
17+
}
18+
19+
# -------------------------------- Control program execution -------------------------------- #
20+
21+
case_to_test = 'real'
22+
part_to_test = 2
23+
verbose_level = 1
24+
25+
# -------------------------------- Initialize some variables -------------------------------- #
26+
27+
puzzle_input = test_data[case_to_test]['input']
28+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
29+
puzzle_actual_result = 'Unknown'
30+
31+
32+
# -------------------------------- Actual code execution -------------------------------- #
33+
34+
35+
count_used = 0
36+
dense_hash = ''
37+
for row in range(128):
38+
current_position = 0
39+
skip_len = 0
40+
rope = list(range(256))
41+
42+
lengths_list = [ord(x) for x in (puzzle_input + '-' + str(row))] + [17, 31, 73, 47, 23]
43+
for i in range (64):
44+
for reverse_length in lengths_list:
45+
if current_position+reverse_length > len(rope):
46+
new_rope = rope[current_position:] + rope[:(current_position+reverse_length) % len(rope)]
47+
new_rope = new_rope[::-1]
48+
rope[current_position:] = new_rope[:len(rope)-current_position]
49+
rope[:(current_position+reverse_length) % len(rope)] = new_rope[len(rope)-current_position:]
50+
else:
51+
new_rope = rope[current_position:current_position+reverse_length]
52+
new_rope = new_rope[::-1]
53+
rope[current_position:current_position+reverse_length] = new_rope
54+
55+
current_position += reverse_length + skip_len
56+
current_position = current_position % len(rope)
57+
skip_len += 1
58+
59+
60+
for i in range (16):
61+
xor_value = reduce(lambda a, b: a^b, rope[i*16:(i+1)*16])
62+
dense_hash += '{0:08b}'.format(xor_value)
63+
64+
dense_hash += '\n'
65+
66+
if part_to_test == 1:
67+
puzzle_actual_result = dense_hash.count('1')
68+
69+
else:
70+
dense_hash = dense_hash.replace('1', '.').replace('0', '#')
71+
72+
graph = pathfinding.Graph()
73+
graph.grid_to_vertices(dense_hash)
74+
75+
nb_groups = 0
76+
cells_in_groups = []
77+
for vertex in graph.vertices:
78+
if vertex in cells_in_groups:
79+
continue
80+
81+
nb_groups += 1
82+
83+
graph.reset_search()
84+
graph.breadth_first_search(vertex)
85+
cells_in_groups += list(graph.distance_from_start.keys())
86+
87+
88+
puzzle_actual_result = nb_groups
89+
90+
91+
# -------------------------------- Outputs / results -------------------------------- #
92+
93+
if verbose_level >= 3:
94+
print ('Input : ' + puzzle_input)
95+
print ('Expected result : ' + str(puzzle_expected_result))
96+
print ('Actual result : ' + str(puzzle_actual_result))
97+
98+
99+
100+

0 commit comments

Comments
 (0)