| 
7 | 7 | class Solution(solution.Solution):  | 
8 | 8 |  def __init__(self, nr):  | 
9 | 9 |  super().__init__(nr)  | 
10 |  | - self.sum1 = 0  | 
 | 10 | + self.params["room name"] = "northpole object storage"  | 
 | 11 | + self.sum = 0  | 
11 | 12 | 
 
  | 
12 | 13 |  def calculate(self, test=False):  | 
13 | 14 |  self.read_instructions()  | 
14 |  | - self.calc_part1()  | 
 | 15 | + self.process_room_list()  | 
15 | 16 | 
 
  | 
16 | 17 |  def read_instructions(self):  | 
17 | 18 |  self.read_input(True)  | 
18 | 19 | 
 
  | 
19 |  | - def calc_part1(self):  | 
 | 20 | + def process_room_list(self):  | 
20 | 21 |  for line in self.input:  | 
21 | 22 |  match = re.match('([a-z\-]+)-(\d+)\[([a-z]{5})\]', line)  | 
22 | 23 |  if match:  | 
23 | 24 |  name = match.group(1)  | 
24 |  | - sector_id = match.group(2)  | 
 | 25 | + sector_id = int(match.group(2))  | 
25 | 26 |  checksum = match.group(3)  | 
26 | 27 |  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)  | 
29 | 34 | 
 
  | 
30 | 35 |  @staticmethod  | 
31 |  | - def get_checksum(name):  | 
 | 36 | + def get_checksum(string):  | 
32 | 37 |  d = collections.defaultdict(int)  | 
33 |  | - for c in name:  | 
 | 38 | + for c in string:  | 
34 | 39 |  if c.isalpha():  | 
35 | 40 |  d[c] -= 1  | 
36 | 41 |  s = collections.OrderedDict(sorted(d.items(), key=operator.itemgetter(1, 0)))  | 
37 | 42 |  checksum = "".join(str(x) for x in s)[:5]  | 
38 | 43 |  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  | 
0 commit comments