Skip to content

Commit 040c095

Browse files
committed
Added days 2018-01 and 2018-02
1 parent f99302d commit 040c095

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

2018/01-Chronal Calibration.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 = '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": ['585', '83173'],
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+
puzzle_actual_result = sum(map(int, puzzle_input.splitlines()))
34+
35+
36+
else:
37+
used_frequencies = [0]
38+
frequency = 0
39+
while True:
40+
for string in puzzle_input.split('\n'):
41+
frequency += int(string)
42+
if frequency in used_frequencies:
43+
puzzle_actual_result = frequency
44+
break
45+
used_frequencies.append(frequency)
46+
47+
if puzzle_actual_result != 'Unknown':
48+
break
49+
50+
51+
52+
# -------------------------------- Outputs / results -------------------------------- #
53+
54+
if verbose_level >= 3:
55+
print ('Input : ' + puzzle_input)
56+
print ('Expected result : ' + str(puzzle_expected_result))
57+
print ('Actual result : ' + str(puzzle_actual_result))
58+
59+
60+
61+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """abcdef
8+
bababc
9+
abbcde
10+
abcccd
11+
aabcdd
12+
abcdee
13+
ababab""",
14+
"expected": ['Unknown', 'Unknown'],
15+
}
16+
17+
test = 2
18+
test_data[test] = {"input": """abcde
19+
fghij
20+
klmno
21+
pqrst
22+
fguij
23+
axcye
24+
wvxyz""",
25+
"expected": ['Unknown', 'Unknown'],
26+
}
27+
28+
test = 'real'
29+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
30+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
31+
"expected": ['7688', 'lsrivmotzbdxpkxnaqmuwcchj'],
32+
}
33+
34+
# -------------------------------- Control program execution -------------------------------- #
35+
36+
case_to_test = 'real'
37+
part_to_test = 2
38+
verbose_level = 1
39+
40+
# -------------------------------- Initialize some variables -------------------------------- #
41+
42+
puzzle_input = test_data[case_to_test]['input']
43+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
44+
puzzle_actual_result = 'Unknown'
45+
46+
47+
# -------------------------------- Actual code execution -------------------------------- #
48+
49+
if part_to_test == 1:
50+
count_2_letters = 0
51+
count_3_letters = 0
52+
53+
for string in puzzle_input.split('\n'):
54+
if any(string.count(x) == 2 for x in string):
55+
count_2_letters += 1
56+
if any(string.count(x) == 3 for x in string):
57+
count_3_letters += 1
58+
59+
puzzle_actual_result = count_2_letters*count_3_letters
60+
61+
62+
else:
63+
list_strings = puzzle_input.split('\n')
64+
for string in list_strings:
65+
for i in range(len(string)):
66+
new_strings = [string[:i] + x + string[i+1:] for x in 'azertyuiopqsdfghjklmwxcvbn']
67+
new_strings.remove(string)
68+
if any(x in list_strings for x in new_strings):
69+
puzzle_actual_result = string[:i] + string[i+1:]
70+
break
71+
if puzzle_actual_result != 'Unknown':
72+
break
73+
74+
75+
76+
77+
# -------------------------------- Outputs / results -------------------------------- #
78+
79+
if verbose_level >= 3:
80+
print ('Input : ' + puzzle_input)
81+
print ('Expected result : ' + str(puzzle_expected_result))
82+
print ('Actual result : ' + str(puzzle_actual_result))
83+
84+
85+
86+

0 commit comments

Comments
 (0)