+/AoC

advent of code

git clone https://git.julia.blue/+/AoC

Joseph RyanA lot closer this year! (missing 19 and 22b)0579d0a

master
1.3 KiB43 linesraw
1from operator import ge, sub
2
3ORE, CLAY, OBS, GEO = range(4)
4build = lambda t, r: t[:r] + (t[r] + 1,) + t[r + 1 :]
5take = lambda a, b: tuple(map(sub, a, b))
6better = enough = lambda a, b: all(map(ge, a, b))
7worse = lambda a, b: all(map(lt, a, b))
8
9
10# naive tree search is too slow, have to memoize or eary cutoff somehow
11# is it ever suboptimal to build a geo bot when you can?
12def rec(prices, bots, mats, score, t):
13    # print(t, bots, mats)
14    new_mats = tuple(a + b for a, b in zip(bots, mats))
15    if not t:
16        return score
17    best = score
18    if enough(mats, prices[GEO]):
19        return rec(prices, bots, take(new_mats, prices[GEO]), score + t - 1, t - 1)
20    for i in range(3):
21        if enough(mats, prices[i]):
22            tmp = rec(prices, build(bots, i), take(new_mats, prices[i]), score, t - 1)
23            best = max(best, tmp)
24    best = max(best, rec(prices, bots, new_mats, score, t - 1))
25    return best
26
27
28def best(blueprint):
29    ore, clay, obs_ore, obs_clay, geo_ore, geo_obs = blueprint
30    prices = (
31        (ore, 0, 0),
32        (clay, 0, 0),
33        (obs_ore, obs_clay, 0),
34        (geo_ore, 0, geo_obs),
35    )
36    bots = (1, 0, 0)
37    mats = (0, 0, 0)
38    return rec(prices, bots, mats, 0, 24)
39
40
41blueprints = [tuple(int(x) for x in s.split() if x.isnumeric()) for s in open("small")]
42print(best(blueprints[0]))
43# print(sum(map(best, blueprints)))