-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.py
189 lines (159 loc) · 5.83 KB
/
day19.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
from __future__ import annotations
import enum
import math
import re
from collections import deque
from dataclasses import dataclass
from functools import partial, reduce
from multiprocessing import Pool
from .shared import Solution
MAX_TICKS = 24
RESOURCES = ("ore", "clay", "obsidian")
class Build(enum.StrEnum):
ORE_BOT = "ore"
CLAY_BOT = "clay"
OBSIDIAN_BOT = "obsidian"
GEODE_BOT = "geode"
@dataclass(frozen=True, slots=True)
class Cost:
ore: int = 0
clay: int = 0
obsidian: int = 0
@dataclass(frozen=True, slots=True)
class Blueprint:
number: int
ore: Cost
clay: Cost
obsidian: Cost
geode: Cost
@classmethod
def from_input(cls, blueprint: str) -> Blueprint:
numbers = map(int, re.findall(r"\d+", blueprint))
return cls(
number=next(numbers),
ore=Cost(ore=next(numbers)),
clay=Cost(ore=next(numbers)),
obsidian=Cost(ore=next(numbers), clay=next(numbers)),
geode=Cost(ore=next(numbers), obsidian=next(numbers)),
)
def max_cost(self, build: str) -> int:
return max(
[
getattr(self.ore, build),
getattr(self.clay, build),
getattr(self.obsidian, build),
getattr(self.geode, build),
]
)
@dataclass(frozen=True, slots=True)
class State:
ticks_left: int = MAX_TICKS
ore: int = 0
clay: int = 0
obsidian: int = 0
geodes: int = 0
ore_bots: int = 1
clay_bots: int = 0
obsidian_bots: int = 0
geode_bots: int = 0
def finished(self) -> bool:
return self.ticks_left <= 0
def finish(self) -> State:
return State(
ticks_left=0,
ore=self.ore + (self.ore_bots * self.ticks_left),
clay=self.clay + (self.clay_bots * self.ticks_left),
obsidian=self.obsidian + (self.obsidian_bots * self.ticks_left),
geodes=self.geodes + (self.geode_bots * self.ticks_left),
ore_bots=self.ore_bots,
clay_bots=self.clay_bots,
obsidian_bots=self.obsidian_bots,
geode_bots=self.geode_bots,
)
def main(input_: list[str]) -> Solution:
blueprints = [Blueprint.from_input(line) for line in input_]
with Pool() as pool:
scores = pool.map(find_best_solution, blueprints)
part1 = sum([a.number * b for a, b in zip(blueprints, scores)])
with Pool() as pool:
scores = pool.map(partial(find_best_solution, initial_ticks=32), blueprints[:3])
part2 = reduce(lambda a, b: a * b, scores, 1)
return Solution(part1, part2)
def find_best_solution(blueprint: Blueprint, initial_ticks: int = MAX_TICKS) -> int:
"""DFS to find the best geode count for given blueprint."""
# start = time.perf_counter()
stack = deque([State(ticks_left=initial_ticks)])
discovered = set()
best_so_far = 0
while stack:
current = stack.pop()
if current in discovered:
continue
discovered.add(current)
if prefect_projected_geodes(current) < best_so_far:
# Can't possibly get more geodes than current best
continue
for candidate in candidates(current, blueprint):
if candidate.finished():
best_so_far = max(best_so_far, candidate.geodes)
else:
stack.append(candidate)
# end = time.perf_counter()
# print(f"Search Done {blueprint.number}: {best_so_far} ({end - start:.2f}s)")
return best_so_far
def candidates(state: State, blueprint: Blueprint) -> list[State]:
"""Returns next possible states"""
states = list()
for directive in Build:
if new_state := next_state(state, directive, blueprint):
states.append(new_state)
return states
def next_state(state: State, build: str, blueprint: Blueprint) -> State | None:
"""Generates next state based on build directive"""
# Check that next state is possible
cost = getattr(blueprint, build)
if any(
[
getattr(cost, resource) > 0 and getattr(state, f"{resource}_bots") < 1
for resource in RESOURCES
]
):
return None
# Have more than enough of this bot
if build != Build.GEODE_BOT and (
getattr(state, f"{build}_bots") >= blueprint.max_cost(build)
):
return None
ticks_required = 1 + max(
[
math.ceil(
(getattr(cost, r) - getattr(state, r)) / getattr(state, f"{r}_bots")
)
for r in RESOURCES
if getattr(cost, r) > 0
]
)
ticks_required = max(1, ticks_required)
if ticks_required >= state.ticks_left:
return state.finish()
return State(
ticks_left=state.ticks_left - ticks_required,
ore=state.ore + (state.ore_bots * ticks_required) - cost.ore,
clay=state.clay + (state.clay_bots * ticks_required) - cost.clay,
obsidian=state.obsidian
+ (state.obsidian_bots * ticks_required)
- cost.obsidian,
geodes=state.geodes + (state.geode_bots * ticks_required),
ore_bots=state.ore_bots + (1 if build == Build.ORE_BOT else 0),
clay_bots=state.clay_bots + (1 if build == Build.CLAY_BOT else 0),
obsidian_bots=state.obsidian_bots + (1 if build == Build.OBSIDIAN_BOT else 0),
geode_bots=state.geode_bots + (1 if build == Build.GEODE_BOT else 0),
)
def prefect_projected_geodes(state: State) -> int:
"""End number of geodes assuming a geode bot is built every tick"""
additional = 0
bots = state.geode_bots
for _ in range(state.ticks_left):
additional += bots
bots += 1
return state.geodes + additional