Skip to content

Commit 96bc519

Browse files
committed
Added days 2017-09, 2017-10, 2017-11
1 parent 72b7d89 commit 96bc519

File tree

3 files changed

+342
-0
lines changed

3 files changed

+342
-0
lines changed

2017/09-Stream Processing.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """<{o"i!a,<{i<a>""",
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": ['9251', '4322'],
20+
}
21+
22+
# -------------------------------- Control program execution -------------------------------- #
23+
24+
case_to_test = 'real'
25+
part_to_test = 1
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+
for string in puzzle_input.split('\n'):
38+
old_string = string
39+
new_string = ''
40+
skip = False
41+
for index in range(len(old_string)):
42+
if skip:
43+
skip = False
44+
continue
45+
elif old_string[index] == '!':
46+
skip = True
47+
else:
48+
new_string += old_string[index]
49+
50+
garbage = False
51+
total_garbage = 0
52+
old_string = new_string
53+
new_string = ''
54+
for index in range(len(old_string)):
55+
if old_string[index] == '<' and not garbage:
56+
garbage = True
57+
elif old_string[index] == '>':
58+
garbage = False
59+
elif garbage:
60+
total_garbage += 1
61+
else:
62+
new_string += old_string[index]
63+
64+
old_string = new_string
65+
new_string = ''
66+
total_score = 0
67+
local_score = 0
68+
for index in range(len(old_string)):
69+
if old_string[index] == '{':
70+
local_score += 1
71+
elif old_string[index] == '}':
72+
total_score += local_score
73+
local_score -= 1
74+
75+
if part_to_test == 1:
76+
puzzle_actual_result = total_score
77+
else:
78+
puzzle_actual_result = total_garbage
79+
80+
81+
# -------------------------------- Outputs / results -------------------------------- #
82+
83+
if verbose_level >= 3:
84+
print ('Input : ' + puzzle_input)
85+
print ('Expected result : ' + str(puzzle_expected_result))
86+
print ('Actual result : ' + str(puzzle_actual_result))
87+
88+
89+
90+

2017/10-Knot Hash.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
from functools import reduce
4+
5+
test_data = {}
6+
7+
test = 1
8+
test_data[test] = {"input": (range(0, 5), '3,4,1,5'),
9+
"expected": ['Unknown', '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": (range(0, 256), open(input_file, "r+").read().strip()),
20+
"expected": ['19591', '62e2204d2ca4f4924f6e7a80f1288786'],
21+
}
22+
23+
# -------------------------------- Control program execution -------------------------------- #
24+
25+
case_to_test = 'real'
26+
part_to_test = 1
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+
if part_to_test == 1:
39+
current_position = 0
40+
skip_len = 0
41+
rope = list(puzzle_input[0])
42+
43+
for reverse_length in puzzle_input[1].split(','):
44+
reverse_length = int(reverse_length)
45+
46+
if current_position+reverse_length > len(rope):
47+
new_rope = rope[current_position:] + rope[:(current_position+reverse_length) % len(rope)]
48+
new_rope = new_rope[::-1]
49+
rope[current_position:] = new_rope[:len(rope)-current_position]
50+
rope[:(current_position+reverse_length) % len(rope)] = new_rope[len(rope)-current_position:]
51+
else:
52+
new_rope = rope[current_position:current_position+reverse_length]
53+
new_rope = new_rope[::-1]
54+
rope[current_position:current_position+reverse_length] = new_rope
55+
56+
current_position += reverse_length + skip_len
57+
current_position = current_position % len(rope)
58+
skip_len += 1
59+
60+
puzzle_actual_result = rope[0] * rope[1]
61+
62+
63+
else:
64+
current_position = 0
65+
skip_len = 0
66+
rope = list(puzzle_input[0])
67+
68+
for i in range (64):
69+
70+
lengths_list = [ord(x) for x in puzzle_input[1]] + [17, 31, 73, 47, 23]
71+
72+
for reverse_length in lengths_list:
73+
if current_position+reverse_length > len(rope):
74+
new_rope = rope[current_position:] + rope[:(current_position+reverse_length) % len(rope)]
75+
new_rope = new_rope[::-1]
76+
rope[current_position:] = new_rope[:len(rope)-current_position]
77+
rope[:(current_position+reverse_length) % len(rope)] = new_rope[len(rope)-current_position:]
78+
else:
79+
new_rope = rope[current_position:current_position+reverse_length]
80+
new_rope = new_rope[::-1]
81+
rope[current_position:current_position+reverse_length] = new_rope
82+
83+
current_position += reverse_length + skip_len
84+
current_position = current_position % len(rope)
85+
skip_len += 1
86+
87+
dense_hash = ''
88+
for i in range (16):
89+
xor_value = reduce(lambda a, b: a^b, rope[i*16:i*16+16])
90+
dense_hash += '%02x'%xor_value
91+
92+
puzzle_actual_result = dense_hash
93+
94+
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/11-Hex Ed.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """ne,ne,ne""",
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": ['877', '1622'],
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+
if part_to_test == 1:
33+
for string in puzzle_input.split('\n'):
34+
# Simplifies counting
35+
string = string.split(',')
36+
nb_se = string.count('se')
37+
nb_sw = string.count('sw')
38+
nb_s = string.count('s')
39+
nb_ne = string.count('ne')
40+
nb_nw = string.count('nw')
41+
nb_n = string.count('n')
42+
43+
# This just makes sure all conversions are done twice (as they influence each other)
44+
for i in range (2):
45+
46+
# Convert se+sw in s
47+
nb_s += min(nb_se, nb_sw)
48+
nb_se, nb_sw = nb_se - min(nb_se, nb_sw), nb_sw - min(nb_se, nb_sw)
49+
50+
# Convert ne+nw in n
51+
nb_n += min(nb_ne, nb_nw)
52+
nb_ne, nb_nw = nb_ne - min(nb_ne, nb_nw), nb_nw - min(nb_ne, nb_nw)
53+
54+
# Convert sw+n in nw
55+
nb_nw += min(nb_sw, nb_n)
56+
nb_sw, nb_n = nb_sw - min(nb_sw, nb_n), nb_n - min(nb_sw, nb_n)
57+
58+
# Convert nw+s in sw
59+
nb_sw += min(nb_nw, nb_s)
60+
nb_nw, nb_s = nb_nw - min(nb_nw, nb_s), nb_s - min(nb_nw, nb_s)
61+
62+
# Convert se+n in ne
63+
nb_ne += min(nb_se, nb_n)
64+
nb_se, nb_n = nb_se - min(nb_se, nb_n), nb_n - min(nb_se, nb_n)
65+
66+
# Convert ne+s in se
67+
nb_se += min(nb_ne, nb_s)
68+
nb_ne, nb_s = nb_ne - min(nb_ne, nb_s), nb_s - min(nb_ne, nb_s)
69+
70+
# Cancel ne and sw
71+
nb_ne, nb_sw = nb_ne - min(nb_ne, nb_sw), nb_sw - min(nb_ne, nb_sw)
72+
73+
# Cancel nw and se
74+
nb_nw, nb_se = nb_nw - min(nb_nw, nb_se), nb_se - min(nb_ne, nb_se)
75+
76+
# Cancel n and s
77+
nb_n, nb_s = nb_n - min(nb_n, nb_s), nb_s - min(nb_n, nb_s)
78+
79+
puzzle_actual_result = sum([nb_se, nb_sw, nb_s, nb_ne, nb_nw, nb_n])
80+
81+
else:
82+
max_distance = 0
83+
84+
all_steps = puzzle_input.split(',')
85+
86+
for i in range (len(all_steps)):
87+
steps = all_steps[0:i+1]
88+
89+
nb_se = steps.count('se')
90+
nb_sw = steps.count('sw')
91+
nb_s = steps.count('s')
92+
nb_ne = steps.count('ne')
93+
nb_nw = steps.count('nw')
94+
nb_n = steps.count('n')
95+
96+
# This just makes sure all conversions are done twice (as they influence each other)
97+
for i in range (2):
98+
99+
# Convert se+sw in s
100+
nb_s += min(nb_se, nb_sw)
101+
nb_se, nb_sw = nb_se - min(nb_se, nb_sw), nb_sw - min(nb_se, nb_sw)
102+
103+
# Convert ne+nw in n
104+
nb_n += min(nb_ne, nb_nw)
105+
nb_ne, nb_nw = nb_ne - min(nb_ne, nb_nw), nb_nw - min(nb_ne, nb_nw)
106+
107+
# Convert sw+n in nw
108+
nb_nw += min(nb_sw, nb_n)
109+
nb_sw, nb_n = nb_sw - min(nb_sw, nb_n), nb_n - min(nb_sw, nb_n)
110+
111+
# Convert nw+s in sw
112+
nb_sw += min(nb_nw, nb_s)
113+
nb_nw, nb_s = nb_nw - min(nb_nw, nb_s), nb_s - min(nb_nw, nb_s)
114+
115+
# Convert se+n in ne
116+
nb_ne += min(nb_se, nb_n)
117+
nb_se, nb_n = nb_se - min(nb_se, nb_n), nb_n - min(nb_se, nb_n)
118+
119+
# Convert ne+s in se
120+
nb_se += min(nb_ne, nb_s)
121+
nb_ne, nb_s = nb_ne - min(nb_ne, nb_s), nb_s - min(nb_ne, nb_s)
122+
123+
# Cancel ne and sw
124+
nb_ne, nb_sw = nb_ne - min(nb_ne, nb_sw), nb_sw - min(nb_ne, nb_sw)
125+
126+
# Cancel nw and se
127+
nb_nw, nb_se = nb_nw - min(nb_nw, nb_se), nb_se - min(nb_ne, nb_se)
128+
129+
# Cancel n and s
130+
nb_n, nb_s = nb_n - min(nb_n, nb_s), nb_s - min(nb_n, nb_s)
131+
132+
max_distance = max(max_distance, sum([nb_se, nb_sw, nb_s, nb_ne, nb_nw, nb_n]))
133+
134+
puzzle_actual_result = max_distance
135+
136+
137+
138+
# -------------------------------- Outputs / results -------------------------------- #
139+
140+
if verbose_level >= 3:
141+
print ('Input : ' + puzzle_input)
142+
print ('Expected result : ' + str(puzzle_expected_result))
143+
print ('Actual result : ' + str(puzzle_actual_result))
144+
145+
146+
147+

0 commit comments

Comments
 (0)