Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2795ab5

Browse files
committed
Day 04: Solution of part 1
1 parent 8772444 commit 2795ab5

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

‎04/log.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,20 @@ not-a-real-room-404[oarel] is a real room.
1212
totally-real-room-200[decoy] is not.
1313
Of the real rooms from the list above, the sum of their sector IDs is 1514.
1414

15-
What is the sum of the sector IDs of the real rooms?
15+
What is the sum of the sector IDs of the real rooms?
16+
17+
Your puzzle answer was 409147.
18+
19+
The first half of this puzzle is complete! It provides one gold star: *
20+
21+
--- Part Two ---
22+
23+
With all the decoy data out of the way, it's time to decrypt this list and get moving.
24+
25+
The room names are encrypted by a state-of-the-art shift cipher, which is nearly unbreakable without the right software. However, the information kiosk designers at Easter Bunny HQ were not expecting to deal with a master cryptographer like yourself.
26+
27+
To decrypt a room name, rotate each letter forward through the alphabet a number of times equal to the room's sector ID. A becomes B, B becomes C, Z becomes A, and so on. Dashes become spaces.
28+
29+
For example, the real name for qzmt-zixmtkozy-ivhz-343 is very encrypted name.
30+
31+
What is the sector ID of the room where North Pole objects are stored?

‎04/solution.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
from lib import solution
2+
import re
3+
import collections
4+
import operator
25

36

47
class Solution(solution.Solution):
58
def __init__(self, nr):
69
super().__init__(nr)
10+
self.sum1 = 0
711

812
def calculate(self, test=False):
913
self.read_instructions()
14+
self.calc_part1()
1015

1116
def read_instructions(self):
1217
self.read_input(True)
18+
19+
def calc_part1(self):
20+
for line in self.input:
21+
match = re.match('([a-z\-]+)-(\d+)\[([a-z]{5})\]', line)
22+
if match:
23+
name = match.group(1)
24+
sector_id = match.group(2)
25+
checksum = match.group(3)
26+
if self.get_checksum(name) == checksum:
27+
self.sum1 += int(sector_id)
28+
self.set_solution(1, self.sum1)
29+
30+
@staticmethod
31+
def get_checksum(name):
32+
d = collections.defaultdict(int)
33+
for c in name:
34+
if c.isalpha():
35+
d[c] -= 1
36+
s = collections.OrderedDict(sorted(d.items(), key=operator.itemgetter(1, 0)))
37+
checksum = "".join(str(x) for x in s)[:5]
38+
return checksum

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /