-
Notifications
You must be signed in to change notification settings - Fork 0
/
adventOfCode22d11.py
261 lines (212 loc) · 10.2 KB
/
adventOfCode22d11.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 11 13:24:13 2022
@author: xBubblex
"""
import math
import re
import numpy as np
def load_files():
fContent = []
with open("input11.txt") as f:
for (lineIndex, line) in enumerate(f): #loading the file into an np.array
if bool(line):
fContent.append(line.strip("\n").split(" "))
return(fContent)
# print(load_files())
class Monkey:
def __init__(self, number = 0, items = [], operation = ["+", "old, old"], test = [2, 0, 1], inspected = 0):
self.number = number
self.items = items
self.operation = operation
self.test = test
self.inspected = inspected
def check(self):
# print("Monkey ", self.number)
pass
class Shenanigans:
def __init__(self):
self.rawMonkeys = load_files()
self.noMonkeys = 0
self.monkeys = []
self.monkeyBusiness = 0
self.lcm = 0
def __count_monkeys(self):
for line in self.rawMonkeys:
if "Monkey" in line:
self.noMonkeys = int(line[1].strip(": ")) if int(line[1].strip(": ")) > self.noMonkeys else 0
return self
def __lcm(self):
self.lcm = np.lcm.reduce([monkey.test[0] for monkey in self.monkeys])
return self
def prepare_monkeys(self):
self.__count_monkeys()
# print(self.rawMonkeys)
number = 0
items = []
operation = []
test = []
numberFound = False
itemsFound = False
operationFound = False
testFound = False
for lineIdx, line in enumerate(self.rawMonkeys):
# print(line)
if "Monkey" in line:
number = int(line[1].strip(": "))
numberFound = True
if "items:" in line:
# print(line.index("items:") + 1)
# print(line.index(line[-1]))
# print([idx for idx in range(line.index("items:"), line.index(line[-1]) + 1)])
items = [int(line[idx].strip(",")) for idx in range(line.index("items:") + 1, line.index(line[-1]) + 1)]
itemsFound = True
if "Operation:" in line:
if bool("+" in line):
operation = ["+", line[line.index("+") - 1], line[line.index("+") + 1]]
if bool("*" in line):
operation = ["*", line[line.index("*") - 1], line[line.index("*") + 1]]
# print(operation)
operationFound = True
if "Test:" in line:
test = [int(line[1 + line.index("by")]), int(self.rawMonkeys[lineIdx + 1][-1]), int(self.rawMonkeys[lineIdx + 2][-1])]
testFound = True
if numberFound and itemsFound and operationFound and testFound:
numberFound = False
itemsFound = False
operationFound = False
testFound = False
monkey = Monkey(number, items, operation, test)
# print(monkey.check())
self.monkeys.append(monkey)
return self
def __monkey_business(self):
monkeysBusinesses = [monkey.inspected for monkey in self.monkeys]
print(monkeysBusinesses)
monkey1 = monkeysBusinesses.pop(monkeysBusinesses.index(max(monkeysBusinesses)))
monkey2 = monkeysBusinesses.pop(monkeysBusinesses.index(max(monkeysBusinesses)))
self.monkeyBusiness = monkey1 * monkey2
return self
def rnd(self, rounds = 20):
self.__lcm()
divider = self.lcm
# print(divider, 19*13*23*17)
def worry_lvl(inspectedItem, operation, rounds):
# print(inspectedItem, operation)
worryLvl = 0
worryLvl = inspectedItem
# print(worryLvl)
# print(worryLvl)
# print(worryLvl) if worryLvl > 0 else 0
# print(operation[1], operation[2])
if operation[0] == "+":
# print("add")
worryLvl = int(worryLvl if operation[1] == "old" else int(operation[1])) + int(worryLvl if operation[2] == "old" else int(operation[2]))
if operation[0] == "*":
# print("multiply")
# num1 = (worryLvl if operation[1] == "old" else int(operation[1]))
# num2 = (worryLvl if operation[2] == "old" else int(operation[2]))
# worryLvl = num1 * num2
# worryLvl = np.multiply((worryLvl if operation[1] == "old" else int(operation[1])), (worryLvl if operation[2] == "old" else int(operation[2])))
worryLvl = int(worryLvl if operation[1] == "old" else int(operation[1])) * int(worryLvl if operation[2] == "old" else int(operation[2]))
# print(worryLvl) if worryLvl > 0 else 0
# print(rounds)
if rounds == 20:
# print("yes")
worryLvl = worryLvl // 3
else:
worryLvl = worryLvl % divider
pass
return worryLvl
def divisibility_rules(worryLv, number):
worryLvl = worryLv
divisible = False
if number == 2:
divisible = True if int(str(worryLvl)[-1]) % 2 == 0 else False
while worryLvl/2 % 2 == 0 and len(str(worryLvl)) > 20:
worryLvl = worryLvl/2
if number == 3:
while len(str(worryLvl)) > 50:
worryLvl = sum([int(digit) for digit in str(worryLvl)])
divisible = True if worryLvl % 3 == 0 else False
if number == 5:
divisible = True if str(worryLvl)[-1] in ["0", "5"] else False
while worryLvl/5 % 5 == 0 and len(str(worryLvl)) > 20:
worryLvl = worryLvl/5
# print(str(worryLvl)[-1] in ["0", "5"])
if number == 7:
if len(str(worryLvl)) > 50:
while len(str(worryLvl)) > 50:
worryLvl = int(str(worryLvl)[:-1]) - 2 * int(str(worryLvl)[-1])
divisible = True if worryLvl % 7 == 0 else False
if number == 11:
if len(str(worryLvl)) > 5:
while len(str(worryLvl)) > 50:
worryLvl = sum([int(digit) for digit in str(worryLvl)[::2]]) - sum([int(digit) for digit in str(worryLvl)[1::2]])
divisible = True if worryLvl % 11 == 0 else False
if number == 13:
if len(str(worryLvl)) > 5:
while len(str(worryLvl)) > 50:
digit = int(list(str(worryLvl)).pop())
worryLvl = int(str(worryLvl)[:-1]) + 4 * digit
# print(len(str(worryLvl)))
divisible = True if worryLvl % 13 == 0 else False
if number == 17:
if len(str(worryLvl)) > 5:
while len(str(worryLvl)) > 50:
digit = int(list(str(worryLvl)).pop())
worryLvl = int(str(worryLvl)[:-1]) - 5 * digit
divisible = True if worryLvl % 17 == 0 else False
if number == 19:
if len(str(worryLvl)) > 5:
while len(str(worryLvl)) > 50:
digit = int(list(str(worryLvl)).pop())
worryLvl = int(str(worryLvl)[:-1]) + 2 * digit
divisible = True if worryLvl % 19 == 0 else False
# print(divisible) if divisible == True else 0
# print(type(number))
if number == 23:
if len(str(worryLvl)) > 5:
while len(str(worryLvl)) > 50:
digit = int(list(str(worryLvl)).pop())
worryLvl = int(str(worryLvl)[:-1]) + 7 * digit
divisible = True if worryLvl % 23 == 0 else False
# print(divisible) if divisible == True else 0
# print(type(number))
return divisible
for roundsIdx, roundd in enumerate(range(rounds)):
# print("\rRound %i" % roundsIdx) if roundsIdx % 100 == 0 else 0
# print("0", self.monkeys[0].items, "\n", "1", self.monkeys[1].items, "\n", "2", self.monkeys[2].items, "\n", "2", self.monkeys[3].items)
for monkeyIdx, monkey in enumerate(self.monkeys):
while len(monkey.items) > 0:
# print(monkey.items)
inspectedItem = monkey.items.pop(0)
# print(inspectedItem)
monkey.inspected += 1
# print(monkey.items)
worryLvl = worry_lvl(inspectedItem, monkey.operation, rounds)
# print(worryLvl) if worryLvl != float("inf") else 0
# self.monkeys[int(monkey.test[2])].items.append(worryLvl) if worryLvl % monkey.test[0] else self.monkeys[int(monkey.test[1])].items.append(worryLvl)
# print(roundsIdx, len(str(worryLvl))) if roundsIdx > 300 else 0
# print(monkey.number, monkey.test) if len(str(worryLvl)) > 10000 else 0
self.monkeys[int(monkey.test[2])].items.append(worryLvl) if not divisibility_rules(worryLvl, monkey.test[0]) else self.monkeys[int(monkey.test[1])].items.append(worryLvl)
del worryLvl
self.__monkey_business()
return self
def run():
monkeys = Shenanigans()
print(monkeys.prepare_monkeys().rnd(10000).monkeyBusiness)
print(run())
# x = [1, 2, 3]
# a = x.pop()
# print(a)
# print(x)
#
# a = "1234567890"
#
# print(int(a[1::2]))
# print(a)
# digit = list(a).pop()
# print(digit, a[:-1])
# print(a)
# print(len(a))