Part 1 | Part 2 | Total | |
---|---|---|---|
Time | >24h | 33:12 | >24h |
I just wasn't getting it. It took a lot of thinking and browsing the r/adventofcode Subreddit, but at last I completed it. Part 1 took a few minutes to run, and Part 2 took a little under 25 minutes to run. Completed on 12/24.
from helpers.datagetter import data_in, submit
from collections import defaultdict
import time
ans = 0
din = data_in(split=True, numbers=True)
def run_blueprint(bp):
state = [1, [1, 0, 0, 0], [0, 0, 0, 0]]
best = 0
saved = defaultdict(int)
def dfs(mins, robots, wallet):
nonlocal best
if not saved[(mins, tuple(robots), tuple(wallet))]:
saved[(mins, tuple(robots), tuple(wallet))] = 1
else:
return
if wallet[3] > best:
best = wallet[3]
print(mins, best)
if mins == 25:
return
if mins == 24:
dfs(mins + 1, [robots[0], robots[1], robots[2], robots[3]],
[wallet[0] + robots[0], wallet[1] + robots[1], wallet[2] + robots[2], wallet[3] + robots[3]])
# Always buy a geode robot if you can
if wallet[0] >= bp[1] and robots[0] < max(bp[2], bp[3], bp[5]):
# print("BUY ORE")
dfs(mins + 1, [robots[0] + 1, robots[1], robots[2], robots[3]],
[wallet[0] - bp[1] + robots[0], wallet[1] + robots[1], wallet[2] + robots[2], wallet[3] + robots[3]])
if wallet[0] >= bp[2] and robots[1] < bp[4]:
# print("BUY CLAY")
dfs(mins + 1, [robots[0], robots[1] + 1, robots[2], robots[3]],
[wallet[0] - bp[2] + robots[0], wallet[1] + robots[1], wallet[2] + robots[2], wallet[3] + robots[3]])
if wallet[0] >= bp[3] and wallet[1] >= bp[4] and robots[2] < bp[6]:
# print("BUY OBSIDIAN")
dfs(mins + 1, [robots[0], robots[1], robots[2] + 1, robots[3]],
[wallet[0] - bp[3] + robots[0], wallet[1] - bp[4] + robots[1], wallet[2] + robots[2],
wallet[3] + robots[3]])
if wallet[0] >= bp[5] and wallet[2] >= bp[6]:
# print("BUY GEODE")
dfs(mins + 1, [robots[0], robots[1], robots[2], robots[3] + 1],
[wallet[0] - bp[5] + robots[0], wallet[1] + robots[1], wallet[2] - bp[6] + robots[2],
wallet[3] + robots[3]])
dfs(mins + 1, [robots[0], robots[1], robots[2], robots[3]],
[wallet[0] + robots[0], wallet[1] + robots[1], wallet[2] + robots[2], wallet[3] + robots[3]])
dfs(state[0], state[1], state[2])
return best
i = 1
for line in din:
start = time.time()
most = run_blueprint(line)
print(time.time() - start)
print(most)
ans += most * i
print("ANS SO FAR", ans)
i += 1
# print(states)
print()
print(ans)
submit(ans)
Very similar code with minor modifications to get it to run only the first 3 blueprints for 32 minutes and multiply their results. Not worth posting.