-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.py
120 lines (86 loc) · 3.33 KB
/
04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import re
import unittest
from collections import Counter
from string import ascii_lowercase
from functools import reduce
rooms = ['aaaaa-bbb-z-y-x-123[abxyz]', 'a-b-c-d-e-f-g-h-987[abcde]',
'not-a-real-room-404[oarel]', 'totally-real-room-200[decoy]']
class TestCode(unittest.TestCase):
def test_checksum_function(self):
self.assertEqual(check_sum(rooms[0]), 'abxyz')
self.assertEqual(check_sum(rooms[1]), 'abcde')
self.assertEqual(check_sum(rooms[2]), 'oarel')
def test_checksum_calculation(self):
self.assertEqual(calculate_checksum(rooms[0]), 'abxyz')
self.assertEqual(calculate_checksum(rooms[1]), 'abcde')
self.assertEqual(calculate_checksum(rooms[2]), 'oarel')
self.assertNotEqual(calculate_checksum(rooms[3]), 'decoy')
def test_if_room_is_real(self):
self.assertEqual(is_real_room(rooms[0]), True)
self.assertEqual(is_real_room(rooms[3]), False)
def test_getID(self):
self.assertEqual(get_ID(rooms[0]), 123)
self.assertEqual(get_ID(rooms[1]), 987)
def test_sum_values(self):
self.assertEqual(sum_values([1, 2, 3, 4, 5]), 15)
class TestPuzzle(unittest.TestCase):
def test_input(self):
summed = list()
with open('04.txt', 'r') as file:
for line in file:
if is_real_room(line):
summed.append(get_ID(line))
# print(sum_values(summed))
class Decrypt(unittest.TestCase):
def test_position(self):
self.assertEqual(position('a'), 0)
self.assertEqual(position('e'), 4)
self.assertEqual(position('z'), 25)
class TestDecryption(unittest.TestCase):
def test_decryption(self):
thenames = list()
with open('04.txt', 'r') as file:
for room in file:
if is_real_room(room):
id = get_ID(room)
name = ''.join(
[char for char in room if char in ascii_lowercase or char == '-'])
newname = rotate(name, id)
if 'north' in newname.lower():
print(room)
thenames.append(newname)
self.assertEqual(
thenames[0], 'rampaging projectile rabbit training iragn')
def rotate(name, times):
newname = list()
for char in name:
if char == '-':
newname.append(' ')
else:
start = position(char)
end = (start + times) % 26
newname.append(ascii_lowercase[end])
return ''.join(newname)
def position(char):
return ascii_lowercase.index(char)
def sum_values(lst):
return reduce(lambda x, y: x + y, lst)
def is_real_room(room):
if check_sum(room) == calculate_checksum(room):
return True
else:
return False
def get_ID(room):
chars = list(room)
return int(''.join([s for s in chars if s.isdigit()]))
def check_sum(room):
pattern = r'^.*\[(.*)\].*$'
matched = re.match(pattern, room)
return matched.group(1)
def calculate_checksum(room):
number = room.split('[')
# count occurrences of each element
elements = Counter(char for char in number[0] if char in ascii_lowercase)
return ''.join(char for char, count in sorted(elements.items(), key=lambda item: (-item[1], item[0]))[:5])
if __name__ == '__main__':
unittest.main()