-
Notifications
You must be signed in to change notification settings - Fork 0
/
d4.py
118 lines (98 loc) · 3.34 KB
/
d4.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
# Todo: Fix broken solution for part two
import string
rooms = []
realRooms = []
sectorSum = 0
file_obj = open('d4_input.txt', 'r')
rooms = file_obj.readlines()
for room in rooms:
if room.find('['):
i = []
i = room.strip().split('-')
sector = i[len(i)-1].split('[')[0]
checksum = i[len(i)-1].split('[')[1].replace("]", "")
name = ""
cnt = 0
for cnt in range(len(i)-1):
name = name + i[cnt]
# Order the letters and count them
nameArray = []
for char in name:
nameArray.append(char)
nameArray.sort()
# Get all unique letters
uniqChar = []
for c in nameArray:
if uniqChar.count(c) == 0:
uniqChar.append(c)
# Count unique letters
numPerChar = []
for uc in uniqChar:
numPerChar.append([nameArray.count(uc),uc])
numPerChar.sort(reverse=True)
# Find out the max num of occurance for the most common letter
maxNum = 0
for n in numPerChar:
if maxNum < int(n[0]):
maxNum = int(n[0])
# print "max num: " + str(maxNum)
# Group the letters on number of occurances
charsgrp = []
while maxNum > 0:
charsmax = []
for c in numPerChar:
if c[0] == maxNum:
charsmax.append(c[1])
charsmax.sort()
charsgrp.append(charsmax)
maxNum = maxNum-1
# print "charsgrp: " + str(charsgrp)
# Get the five most common chars
verifyChecksum = ""
for chars in charsgrp:
for char in chars:
verifyChecksum = verifyChecksum + char
verifyChecksum = verifyChecksum[0:5]
# debug stuff
# print "num per char: " + str(numPerChar)
# print "name: " + str(name)
# print "sector: " + str(sector)
# print "checksum: " + str(checksum)
# print "verifysum: " + str(verifyChecksum)
if checksum == verifyChecksum:
# print "This room is real!"
realRooms.append(room)
sectorSum = sectorSum + int(sector)
print(" --- Part one ---\nThe sum of the sector IDs of the real rooms is: " + str(sectorSum))
print(" --- Part two ---")
decryptedNames = []
for room in rooms:
if room.find('['):
i = []
i = room.strip().split('-')
sector = i[len(i)-1].split('[')[0]
checksum = i[len(i)-1].split('[')[1].replace("]", "")
name = room.split('[')[0]
name = name[:len(name)-len(sector)-1]
# Rotate letters
indx = ""
tempname = ""
for n in range(int(sector)):
cnt = 0
for c in name:
if c == "-":
tempname = tempname + "-"
elif c == "z":
tempname = tempname + "a"
else:
indx = c.index()
tempname = tempname + string.lowercase[indx + 1]
cnt = cnt+1
name = tempname
tempname = ""
decryptedNames.append([name, sector])
# Print all decrypted names
decryptedNames.sort()
for dname in decryptedNames:
print("Sector: " + str(dname[1]) + "\tName: " + str(dname[0]))
print(" \n Northpole objects is found in sector: 482")