Skip to content

Commit 8e3e2db

Browse files
committed
Added days 2016-02, 2016-02, 2016-03 and 2016-04
1 parent 37a05ef commit 8e3e2db

File tree

5 files changed

+356
-1
lines changed

5 files changed

+356
-1
lines changed

2016/01-No Time for a Taxicab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
test = 'real'
2727
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
2828
test_data[test] = {"input": open(input_file, "r+").read().strip(),
29-
"expected": ['Unknown', 'Unknown'],
29+
"expected": ['273', '115'],
3030
}
3131

3232
# -------------------------------- Control program execution -------------------------------- #

2016/02-Bathroom Security.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """ULL
8+
RRDDD
9+
LURDL
10+
UUUUD""",
11+
"expected": ['1985', '5DB3'],
12+
}
13+
14+
test += 1
15+
test_data[test] = {"input": """""",
16+
"expected": ['Unknown', 'Unknown'],
17+
}
18+
19+
test = 'real'
20+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
21+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
22+
"expected": ['36629', 'Unknown'],
23+
}
24+
25+
# -------------------------------- Control program execution -------------------------------- #
26+
27+
case_to_test = 'real'
28+
part_to_test = 2
29+
verbose_level = 1
30+
31+
# -------------------------------- Initialize some variables -------------------------------- #
32+
33+
puzzle_input = test_data[case_to_test]['input']
34+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
35+
puzzle_actual_result = 'Unknown'
36+
37+
38+
# -------------------------------- Actual code execution -------------------------------- #
39+
40+
password = ''
41+
42+
if part_to_test == 1:
43+
keypad = '''123
44+
456
45+
789'''
46+
47+
x = 1
48+
y = 1
49+
for string in puzzle_input.split('\n'):
50+
for letter in string:
51+
if letter == 'U':
52+
y = max(0, y-1)
53+
elif letter == 'D':
54+
y = min(2, y+1)
55+
elif letter == 'L':
56+
x = max(0, x-1)
57+
elif letter == 'R':
58+
x = min(2, x+1)
59+
60+
password += keypad.split('\n')[y][x]
61+
62+
puzzle_actual_result = password
63+
64+
65+
else:
66+
keypad = '''__1__
67+
_234_
68+
56789
69+
_ABC_
70+
__D__'''
71+
72+
x = 0
73+
y = 2
74+
for string in puzzle_input.split('\n'):
75+
for letter in string:
76+
x_new, y_new = x, y
77+
if letter == 'U':
78+
y_new = max(0, y_new-1)
79+
elif letter == 'D':
80+
y_new = min(4, y_new+1)
81+
elif letter == 'L':
82+
x_new = max(0, x_new-1)
83+
elif letter == 'R':
84+
x_new = min(4, x_new+1)
85+
86+
if not keypad.split('\n')[y_new][x_new] == '_':
87+
x, y = x_new, y_new
88+
89+
password += keypad.split('\n')[y][x]
90+
91+
puzzle_actual_result = password
92+
93+
94+
95+
# -------------------------------- Outputs / results -------------------------------- #
96+
97+
if verbose_level >= 3:
98+
print ('Input : ' + puzzle_input)
99+
print ('Expected result : ' + str(puzzle_expected_result))
100+
print ('Actual result : ' + str(puzzle_actual_result))
101+
102+
103+
104+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """5 10 25
8+
10 15 12""",
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": open(input_file, "r+").read().strip(),
20+
"expected": ['983', 'Unknown'],
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+
possible_triangles = 0
39+
if part_to_test == 1:
40+
for string in puzzle_input.split('\n'):
41+
sides = [int(x) for x in string.split(' ') if not x == '']
42+
sides.sort()
43+
a, b, c = sides
44+
45+
print (string, a, b, c, a+b)
46+
47+
if c < (a + b):
48+
possible_triangles += 1
49+
50+
51+
puzzle_actual_result = possible_triangles
52+
53+
else:
54+
lines = puzzle_input.split('\n')
55+
for n in range(len(lines)):
56+
lines[n] = [int(x) for x in lines[n].split(' ') if not x == '']
57+
for n in range(len(lines)//3):
58+
for i in range (3):
59+
sides = [int(lines[n*3+y][i]) for y in range (3)]
60+
print (lines[n*3:n*3+3])
61+
print(sides)
62+
sides.sort()
63+
a, b, c = sides
64+
65+
if c < (a + b):
66+
possible_triangles += 1
67+
68+
puzzle_actual_result = possible_triangles
69+
70+
71+
72+
# -------------------------------- Outputs / results -------------------------------- #
73+
74+
if verbose_level >= 3:
75+
print ('Input : ' + puzzle_input)
76+
print ('Expected result : ' + str(puzzle_expected_result))
77+
print ('Actual result : ' + str(puzzle_actual_result))
78+
79+
80+
81+
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": """aaaaa-bbb-z-y-x-123[abxyz]
8+
a-b-c-d-e-f-g-h-987[abcde]
9+
not-a-real-room-404[oarel]
10+
totally-real-room-200[decoy]""",
11+
"expected": ['1415', 'Unknown'],
12+
}
13+
14+
test += 1
15+
test_data[test] = {"input": """amppmqgtc-afmamjyrc-bctcjmnkclr-730[jbafl]""",
16+
"expected": ['Unknown', 'Unknown'],
17+
}
18+
19+
test = 'real'
20+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
21+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
22+
"expected": ['173787', '548'],
23+
}
24+
25+
# -------------------------------- Control program execution -------------------------------- #
26+
27+
case_to_test = 'real'
28+
part_to_test = 2
29+
verbose_level = 1
30+
31+
# -------------------------------- Initialize some variables -------------------------------- #
32+
33+
puzzle_input = test_data[case_to_test]['input']
34+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
35+
puzzle_actual_result = 'Unknown'
36+
37+
38+
# -------------------------------- Actual code execution -------------------------------- #
39+
40+
sum_sectors = 0
41+
42+
for string in puzzle_input.split('\n'):
43+
count_letters = {}
44+
if string == '':
45+
continue
46+
for i in range(len(string)):
47+
letter = string[i]
48+
if letter == '-':
49+
continue
50+
elif letter in 'azertyuiopqsdfghjklmwxcvbn':
51+
if letter in count_letters:
52+
count_letters[letter] += 1
53+
else:
54+
count_letters[letter] = 1
55+
elif letter in '0123456789':
56+
sector = int(string[i:i+3])
57+
checksum_real = string[i+4:i+9]
58+
59+
checksum_calc = ''
60+
for count in range (len(string), 0, -1):
61+
letters = [x for x in count_letters if count_letters[x] == count]
62+
if letters:
63+
letters.sort()
64+
checksum_calc += ''.join(letters)
65+
if len(checksum_calc) >= 5:
66+
if checksum_real == checksum_calc[0:5]:
67+
sum_sectors += sector
68+
69+
70+
decrypted_room_name = [chr((ord(letter) - 97 + sector) % 26 + 97) if letter != '-' else ' ' for letter in string]
71+
if 'north' in ''.join(decrypted_room_name):
72+
if part_to_test == 2:
73+
puzzle_actual_result = sector
74+
75+
break
76+
break
77+
if part_to_test == 1:
78+
puzzle_actual_result = sum_sectors
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+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": 'abc',
8+
"expected": ['18f47a30', '05ace8e3'],
9+
}
10+
11+
test += 1
12+
test_data[test] = {"input": """""",
13+
"expected": ['Unknown', 'Unknown'],
14+
}
15+
16+
test = 'real'
17+
test_data[test] = {"input": 'wtnhxymk',
18+
"expected": ['2414bc77', '437e60fc'],
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+
import hashlib
36+
password = ''
37+
38+
encode = hashlib.md5()
39+
40+
if part_to_test == 1:
41+
for i in range (10**10):
42+
coded_value = puzzle_input.encode('utf-8') + str(i).encode('utf-8')
43+
encoded = hashlib.md5(coded_value).hexdigest()
44+
if encoded[0:5] == '00000':
45+
password += encoded[5]
46+
print (i, password, coded_value, encoded)
47+
if len(password) == 8:
48+
puzzle_actual_result = password
49+
break
50+
51+
52+
else:
53+
password = ['_', '_', '_', '_', '_', '_', '_', '_']
54+
for i in range (10**10):
55+
coded_value = puzzle_input.encode('utf-8') + str(i).encode('utf-8')
56+
encoded = hashlib.md5(coded_value).hexdigest()
57+
if encoded[0:5] == '00000':
58+
if encoded[5] in 'azertyuiopqsdfghjklmwxcvbn':
59+
continue
60+
if int(encoded[5]) > 7:
61+
continue
62+
if password[int(encoded[5])] == '_':
63+
password[int(encoded[5])] = encoded[6]
64+
print (i, ''.join(password), coded_value, encoded)
65+
if '_' not in password:
66+
puzzle_actual_result = ''.join(password)
67+
break
68+
69+
70+
71+
# -------------------------------- Outputs / results -------------------------------- #
72+
73+
if verbose_level >= 3:
74+
print ('Input : ' + puzzle_input)
75+
print ('Expected result : ' + str(puzzle_expected_result))
76+
print ('Actual result : ' + str(puzzle_actual_result))
77+
78+
79+
80+

0 commit comments

Comments
 (0)