-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.py
144 lines (116 loc) · 3.6 KB
/
day7.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
141
142
143
144
alphabet = list("qwertyuiopasdfghjklzxcvbnm".upper())
# alphabet = list("abcdef".upper())
def cleanLine(line):
line = line.split()
newLine = [line[1], line[7]]
return newLine
def createArr(x, y):
arr = []
for i in range(x):
arr2 = []
for j in range(y):
arr2.append(0)
arr.append(arr2)
return arr
def currentlyPossible(req, done):
possible = []
for item, needed in req.items():
if len(done) == 0:
if needed == done:
possible.append(item)
else:
okay = True
for thing in needed:
if thing not in done or item in done:
okay = False
if okay == True:
possible.append(item)
for item, needed in req.items():
if item in done:
possible = list(filter((item).__ne__, possible))
return possible
def run(one = True, two = True, inputData = "input/input7.txt"):
# alphabet = list("abcdef".upper())
with open(inputData) as f:
lines = f.readlines()
requirements = {}
for i in range(len(alphabet)):
requirements[alphabet[i]] = []
for line in lines:
line = cleanLine(line)
requirements[line[1]].append(line[0])
requirements[line[1]].sort()
sorted_by_value = sorted(requirements.items(), key=lambda kv: kv[1])
order = []
orderSorted = []
done = False
current = 0
while done == False:
possible = []
for item, needed in requirements.items():
if len(orderSorted) == 0:
if needed == orderSorted:
possible.append(item)
else:
okay = True
for thing in needed:
if thing not in order:
okay = False
if okay == True:
possible.append(item)
newPos = [0, 1]
bestLength = 100
for item in possible:
if item in order:
possible = list(filter((item).__ne__, possible))
continue
possible.sort()
order.append(possible[0])
orderSorted = sorted(order)
if len(order) == len(alphabet):
done = True
break
if one == True:
print("day 7, part 1:", "".join(order))
if two == False:
return
# workers = [0, 0, 0, 0, 0]
workers = [0, 0, 0, 0, 0]
done = []
doing = []
times = [0, 0, 0, 0, 0]
finished = False
total = 0
while finished == False:
possible = currentlyPossible(requirements, done)
if len(possible) == 0:
finished = True
continue
for pos in possible:
i = 0
for worker in workers:
if worker == 0 and pos not in workers:
workers[i] = pos
doing.append(pos)
times[i] = (ord(pos) - 64) + 60
i+=1
timesNoZero = list(times)
timesNoZero = list(filter(lambda a: a != 0, timesNoZero))
mini = min(timesNoZero)
total += mini
for i in range(len(times)):
if times[i] > 0:
times[i] -= mini
if times[i] == 0:
done.append(str(workers[i]))
workers[i] = 0
print("day 7, part 2:", total)
def part1(inputData = "input/input7.txt"):
run(True, False, inputData)
def part2(inputData = "input/input7.txt"):
run(False, True, inputData)
def main(inputData = "input/input7.txt"):
part1(inputData)
part2(inputData)
if __name__ == "__main__":
main()