Skip to content

Commit d6336e5

Browse files
committed
Day 25 - Improve runtime by storing heavy subsets
1 parent 013d5e4 commit d6336e5

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

‎day25.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,23 @@ def run(self, inputValue=None):
153153

154154
items = [s[5:] for s in save if s.startswith("take")]
155155

156+
tooHeavy = [] # Store all sets of items that are too heavy (can't be a subset of the answer)
157+
156158
vm = VM(memory)
157159
vm.run("\n".join(save[:-1])+"\n")
158160
vm.run("\n".join(["drop "+item for item in items])+"\n")
159161

160162
# Bruteforces the pressure floor by trying all item combinations
161163
for i in range(1, 2**len(items)-1):
162164
pick = [int(i) for i in "{:08b}".format(i)]
163-
picked = [item for i, item in enumerate(items) if pick[i]]
164-
# print("Trying items: {}".format(picked))
165+
pickedItems = [item for i, item in enumerate(items) if pick[i]]
166+
# print("Trying items: {}".format(pickedItems))
167+
168+
# Check against the invalid subsets, faster than doing it in-game
169+
if any([set(s).issubset(set(pickedItems)) for s in tooHeavy]): continue
165170

166171
# Pick up all selected items
167-
vm.run("\n".join(["take "+item for item in picked])+"\n")
172+
vm.run("\n".join(["take "+item for item in pickedItems])+"\n")
168173

169174
# Activate floor
170175
vm.run(save[-1]+"\n")
@@ -175,9 +180,11 @@ def run(self, inputValue=None):
175180
# print(output)
176181
print("Part 1: {}".format(output[-43:-37]))
177182
break
183+
elif output.find("lighter") != -1: # Too heavy, add as invalid subset
184+
tooHeavy.append(pickedItems)
178185

179186
# Drop all items that were picked up
180-
vm.run("\n".join(["drop "+item for item in picked])+"\n")
187+
vm.run("\n".join(["drop "+item for item in pickedItems])+"\n")
181188
vm.output = []
182189

183190
AOCUtils.printTimeTaken()

0 commit comments

Comments
 (0)
close