-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.py
140 lines (114 loc) · 3.46 KB
/
day4.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
def cleanLine(line):
line = line.replace("[", "").replace("]", "").replace("#", "")
arr = line.split()
date = arr[0].split("-")
arr.pop(0)
x = 0
for item in date:
arr.insert(x, item)
x += 1
time = arr[3].split(":")
arr.pop(3)
x = 3
for item in time:
arr.insert(x, item)
x += 1
for i in range(0, 5):
arr[i] = int(arr[i])
return arr
def getDataValue(line):
month = line[1]
day = line[2]
hour = line[3]
minute = line[4]
total = (month * 30) + (day) + (hour / 24) + ((minute / 60) / 24)
return total
def part1and2(one = True, two = True, inputData = "input/input4.txt"):
lineInfo = []
data = {}
values = []
lines = 0
with open(inputData) as f:
lines = f.readlines()
newF = open("input/ordered4.txt", "w+")
for i in range(0, len(lines)):
line = cleanLine(lines[i])
value = getDataValue(line)
lineInfo.append(line)
data[value] = i
values.append(value)
values.sort()
for value in values:
lineStr = data[value]
lineStr = lineInfo[lineStr]
for i in range(0, len(lineStr)):
lineStr[i] = str(lineStr[i])
lineStr = " ".join(lineStr)
newF.write(lineStr + "\n")
newF.close()
newF = open("input/ordered4.txt")
linesN = newF.readlines()
currentID = 0
timeAsleep = {}
currentSleep = 0
startSleepM = 0
startSleepH = 0
IDs = []
times = []
minutes = []
for i in range(0, 23):
minute = []
minutes.append(minute)
times.append(0)
for line in linesN:
lineArr = line.split()
for i in range(0, 5):
lineArr[i] = int(lineArr[i])
if lineArr[5] == "Guard":
currentID = lineArr[6]
if currentID not in IDs:
IDs.append(currentID)
if lineArr[5] == "falls":
startSleepM = lineArr[4]
startSleepH = lineArr[3]
if lineArr[5] == "wakes":
differenceM = 0
differenceH = 0
if lineArr[4] > startSleepM:
differenceM = lineArr[4] - startSleepM
for i in range(startSleepM, lineArr[4]):
minutes[IDs.index(currentID)].append(i)
times[IDs.index(currentID)] += differenceM
best = 0
bestIndex = 0
for i in range(0, len(times)):
if times[i] > best:
best = times[i]
bestIndex = i
mostCommon = 0
mostCount = 0
for item in minutes[bestIndex]:
if minutes[bestIndex].count(item) > mostCount:
mostCommon = item
mostCount = minutes[bestIndex].count(item)
if one == True:
print("day 4, part 1: " + str(int(mostCommon) * int(IDs[bestIndex])))
if two == True:
mostCommon = 0
mostCount = 0
mostID = 0
for i in range(0, len(minutes)):
for item in minutes[i]:
if minutes[i].count(item) > mostCount:
mostCount = minutes[i].count(item)
mostCommon = item
mostID = i
print("day 4, part 2: " + str(int(IDs[mostID]) * int(mostCommon)))
def main(inputData = "input/input4.txt"):
part1and2(True, True, inputData)
def part1(inputData = "input/input4.txt"):
part1and2(True, False, inputData)
def part2(inputData = "input/input4.txt"):
part1and2(False, True, inputData)
if __name__ == "__main__":
main()