Skip to content

Commit 5793a2b

Browse files
committed
Added days 2016-06 and 2016-07
1 parent 8e3e2db commit 5793a2b

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

2016/06-Signals and Noise.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """eedadn
8+
drvtee
9+
eandsr
10+
raavrd
11+
atevrs
12+
tsrnev
13+
sdttsa
14+
rasrtv
15+
nssdts
16+
ntnada
17+
svetve
18+
tesnvt
19+
vntsnd
20+
vrdear
21+
dvrsen
22+
enarar""",
23+
"expected": ['Unknown', 'Unknown'],
24+
}
25+
26+
test += 1
27+
test_data[test] = {"input": """""",
28+
"expected": ['Unknown', 'Unknown'],
29+
}
30+
31+
test = 'real'
32+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
33+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
34+
"expected": ['dzqckwsd', 'lragovly'],
35+
}
36+
37+
# -------------------------------- Control program execution -------------------------------- #
38+
39+
case_to_test = 'real'
40+
part_to_test = 1
41+
verbose_level = 1
42+
43+
# -------------------------------- Initialize some variables -------------------------------- #
44+
45+
puzzle_input = test_data[case_to_test]['input']
46+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
47+
puzzle_actual_result = 'Unknown'
48+
49+
50+
# -------------------------------- Actual code execution -------------------------------- #
51+
52+
frequencies = {}
53+
54+
for string in puzzle_input.split('\n'):
55+
if string == '':
56+
continue
57+
for index in range(len(string)):
58+
if not index in frequencies:
59+
frequencies[index] = {}
60+
if not string[index] in frequencies[index]:
61+
frequencies[index][string[index]] = 1
62+
else:
63+
frequencies[index][string[index]] += 1
64+
65+
password = ''
66+
for index in range(len(string)):
67+
if part_to_test == 1:
68+
letter = [x for x in frequencies[index] if frequencies[index][x] == max(frequencies[index].values())]
69+
elif part_to_test == 2:
70+
letter = [x for x in frequencies[index] if frequencies[index][x] == min(frequencies[index].values())]
71+
password += ''.join(letter)
72+
73+
puzzle_actual_result = password
74+
75+
76+
# -------------------------------- Outputs / results -------------------------------- #
77+
78+
if verbose_level >= 3:
79+
print ('Input : ' + puzzle_input)
80+
print ('Expected result : ' + str(puzzle_expected_result))
81+
print ('Actual result : ' + str(puzzle_actual_result))
82+
83+
84+
85+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """abba[mnop]qrst
8+
abcd[bddb]xyyx
9+
aaaa[qwer]tyui
10+
ioxxoj[asdfgh]zxcvbn""",
11+
"expected": ['Unknown', 'Unknown'],
12+
}
13+
14+
test += 1
15+
test_data[test] = {"input": """aba[bab]xyz
16+
xyx[xyx]xyx
17+
aaa[kek]eke
18+
zazbz[bzb]cdb""",
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": ['115', 'Unknown'],
26+
}
27+
28+
# -------------------------------- Control program execution -------------------------------- #
29+
30+
case_to_test = 'real'
31+
part_to_test = 2
32+
verbose_level = 1
33+
34+
# -------------------------------- Initialize some variables -------------------------------- #
35+
36+
puzzle_input = test_data[case_to_test]['input']
37+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
38+
puzzle_actual_result = 'Unknown'
39+
40+
41+
# -------------------------------- Actual code execution -------------------------------- #
42+
43+
if part_to_test == 1:
44+
count_abba = 0
45+
for string in puzzle_input.split('\n'):
46+
abba = False
47+
if string == '':
48+
continue
49+
50+
in_brackets = False
51+
52+
for index in range(len(string)-3):
53+
if string[index] == '[':
54+
in_brackets = True
55+
continue
56+
elif string[index] == ']':
57+
in_brackets = False
58+
continue
59+
60+
if string[index] == string[index+3] and string[index+1] == string[index+2] and string[index] != string[index+1]:
61+
if in_brackets:
62+
abba = False
63+
break
64+
else:
65+
abba = True
66+
if abba:
67+
count_abba += 1
68+
puzzle_actual_result = count_abba
69+
70+
else:
71+
ssl_support = 0
72+
for string in puzzle_input.split('\n'):
73+
aba_sequences = []
74+
bab_sequences = []
75+
if string == '':
76+
continue
77+
78+
in_brackets = False
79+
80+
for index in range(len(string)-2):
81+
if string[index] == '[':
82+
in_brackets = True
83+
continue
84+
elif string[index] == ']':
85+
in_brackets = False
86+
continue
87+
88+
if string[index] == string[index+2] and string[index] != string[index+1]:
89+
if in_brackets:
90+
aba_sequences.append(string[index:index+3])
91+
else:
92+
bab_sequences.append(string[index:index+3])
93+
matching = [x for x in aba_sequences if x[1] + x[0] + x[1] in bab_sequences]
94+
95+
if matching:
96+
ssl_support += 1
97+
puzzle_actual_result = ssl_support
98+
99+
100+
# -------------------------------- Outputs / results -------------------------------- #
101+
102+
if verbose_level >= 3:
103+
print ('Input : ' + puzzle_input)
104+
print ('Expected result : ' + str(puzzle_expected_result))
105+
print ('Actual result : ' + str(puzzle_actual_result))
106+
107+
108+
109+

0 commit comments

Comments
 (0)