Skip to content

Commit 6637097

Browse files
committed
Day 04: Solution of part 2
1 parent 86ba022 commit 6637097

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

04/log.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ What is the sum of the sector IDs of the real rooms?
1616

1717
Your puzzle answer was 409147.
1818

19-
The first half of this puzzle is complete! It provides one gold star: *
20-
2119
--- Part Two ---
2220

2321
With all the decoy data out of the way, it's time to decrypt this list and get moving.
@@ -28,4 +26,8 @@ To decrypt a room name, rotate each letter forward through the alphabet a number
2826

2927
For example, the real name for qzmt-zixmtkozy-ivhz-343 is very encrypted name.
3028

31-
What is the sector ID of the room where North Pole objects are stored?
29+
What is the sector ID of the room where North Pole objects are stored?
30+
31+
Your puzzle answer was 991.
32+
33+
Both parts of this puzzle are complete! They provide two gold stars: **

04/solution.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,47 @@
77
class Solution(solution.Solution):
88
def __init__(self, nr):
99
super().__init__(nr)
10-
self.sum1 = 0
10+
self.params["room name"] = "northpole object storage"
11+
self.sum = 0
1112

1213
def calculate(self, test=False):
1314
self.read_instructions()
14-
self.calc_part1()
15+
self.process_room_list()
1516

1617
def read_instructions(self):
1718
self.read_input(True)
1819

19-
def calc_part1(self):
20+
def process_room_list(self):
2021
for line in self.input:
2122
match = re.match('([a-z\-]+)-(\d+)\[([a-z]{5})\]', line)
2223
if match:
2324
name = match.group(1)
24-
sector_id = match.group(2)
25+
sector_id = int(match.group(2))
2526
checksum = match.group(3)
2627
if self.get_checksum(name) == checksum:
27-
self.sum1 += int(sector_id)
28-
self.set_solution(1, self.sum1)
28+
self.sum += sector_id
29+
name_decrypted = self.decrypt_name(name, sector_id)
30+
# print(name_decrypt)
31+
if name_decrypted == self.params["room name"]:
32+
self.set_solution(2, sector_id)
33+
self.set_solution(1, self.sum)
2934

3035
@staticmethod
31-
def get_checksum(name):
36+
def get_checksum(string):
3237
d = collections.defaultdict(int)
33-
for c in name:
38+
for c in string:
3439
if c.isalpha():
3540
d[c] -= 1
3641
s = collections.OrderedDict(sorted(d.items(), key=operator.itemgetter(1, 0)))
3742
checksum = "".join(str(x) for x in s)[:5]
3843
return checksum
44+
45+
@staticmethod
46+
def decrypt_name(name, shift):
47+
decrypted = ""
48+
for c in name:
49+
if c.isalpha():
50+
decrypted += chr((ord(c.lower()) - 97 + shift) % 26 + 97)
51+
if c == "-":
52+
decrypted += " "
53+
return decrypted

04/test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ def test_1(self):
1515
"""
1616
self.execute_test(test_input)
1717
self.assertEqual(1514, self.solution.get_solution(1))
18+
19+
def test_2(self):
20+
test_input = """qzmt-zixmtkozy-ivhz-343[zimth]"""
21+
self.set_param("room name", "very encrypted name")
22+
self.execute_test(test_input)
23+
self.assertEqual(343, self.get_solution(2))

0 commit comments

Comments
 (0)