/
day19.jl
226 lines (167 loc) · 6.2 KB
/
day19.jl
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
println("Day 19")
struct Blueprint
id::Int
cost_ore_bot::Int
cost_clay_bot::Int
cost_obs_bot::Tuple{Int, Int}
cost_geode_bot::Tuple{Int, Int}
end
struct State
time::Int
n_ore::Int
n_clay::Int
n_obs::Int
n_geode::Int
n_ore_bot::Int
n_clay_bot::Int
n_obs_bot::Int
n_geode_bot::Int
skipped_ore_bot::Bool
skipped_clay_bot::Bool
skipped_obs_bot::Bool
end
function parse_input(filepath::String)
blueprints = Blueprint[]
for l in readlines(filepath)
s = String.(split(l, [' ', ':']))
id = parse(Int, s[2])
cost_ore_bot = parse(Int, s[8])
cost_clay_bot = parse(Int, s[14])
cost_obs_bot = parse.(Int, (s[20], s[23]))
cost_geode_bot = parse.(Int, (s[29], s[32]))
blueprint = Blueprint(id, cost_ore_bot, cost_clay_bot,
cost_obs_bot, cost_geode_bot)
push!(blueprints, blueprint)
end
return blueprints
end
function collect_resources(state::State)
new_time = state.time + 1
new_n_ore = state.n_ore + state.n_ore_bot
new_n_clay = state.n_clay + state.n_clay_bot
new_n_obs = state.n_obs + state.n_obs_bot
new_n_geode = state.n_geode + state.n_geode_bot
return State(new_time, new_n_ore, new_n_clay, new_n_obs, new_n_geode,
state.n_ore_bot, state.n_clay_bot, state.n_obs_bot, state.n_geode_bot,
state.skipped_ore_bot, state.skipped_clay_bot, state.skipped_obs_bot)
end
function check_resource(option::Char, state::State, blueprint::Blueprint)
if (state.n_ore >= blueprint.cost_geode_bot[1]) &&
(state.n_obs >= blueprint.cost_geode_bot[2])
return option == 'g'
else
if option == 'n'
return true
elseif option == 'o'
return (!state.skipped_ore_bot) &&
state.n_ore >= blueprint.cost_ore_bot
elseif option == 'c'
return (!state.skipped_clay_bot) &&
state.n_ore >= blueprint.cost_clay_bot
elseif option == 'b'
return (!state.skipped_obs_bot) &&
(state.n_ore >= blueprint.cost_obs_bot[1]) &&
(state.n_clay >= blueprint.cost_obs_bot[2])
elseif option == 'g'
return false
end
end
end
function max_possible_geodes(state::State, limit::Int)
time_remaining = limit - state.time
return state.n_geode +
time_remaining * state.n_geode_bot +
div(time_remaining * (time_remaining - 1), 2)
end
function decision(option::Char, state::State, blueprint::Blueprint)
skipped_ore_bot = (option == 'n') && check_resource('o', state, blueprint)
skipped_clay_bot = (option == 'n') && check_resource('c', state, blueprint)
skipped_obs_bot = (option == 'n') && check_resource('b', state, blueprint)
new_state = State(state.time, state.n_ore, state.n_clay, state.n_obs, state.n_geode,
state.n_ore_bot, state.n_clay_bot, state.n_obs_bot, state.n_geode_bot,
skipped_ore_bot, skipped_clay_bot, skipped_obs_bot)
new_state = collect_resources(new_state)
new_state = build_robot(option, new_state, blueprint)
return new_state
end
function build_robot(option::Char, state::State, blueprint::Blueprint)
new_n_ore = state.n_ore
new_n_clay = state.n_clay
new_n_obs = state.n_obs
new_n_geode = state.n_geode
new_n_ore_bot = state.n_ore_bot
new_n_clay_bot = state.n_clay_bot
new_n_obs_bot = state.n_obs_bot
new_n_geode_bot = state.n_geode_bot
if option == 'n'
nothing
elseif option == 'o'
new_n_ore -= blueprint.cost_ore_bot
new_n_ore_bot += 1
elseif option == 'c'
new_n_ore -= blueprint.cost_clay_bot
new_n_clay_bot += 1
elseif option == 'b'
new_n_ore -= blueprint.cost_obs_bot[1]
new_n_clay -= blueprint.cost_obs_bot[2]
new_n_obs_bot += 1
elseif option == 'g'
new_n_ore -= blueprint.cost_geode_bot[1]
new_n_obs -= blueprint.cost_geode_bot[2]
new_n_geode_bot += 1
end
return State(state.time, new_n_ore, new_n_clay, new_n_obs, new_n_geode,
new_n_ore_bot, new_n_clay_bot, new_n_obs_bot, new_n_geode_bot,
state.skipped_ore_bot, state.skipped_clay_bot, state.skipped_obs_bot)
end
function get_most_geodes(blueprint::Blueprint, limit::Int)
most_geodes::Int = 0
options = Char['n', 'o', 'c', 'b', 'g']
checking = State[State(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0)]
checked = State[]
max_required_ore::Int = max(blueprint.cost_ore_bot, blueprint.cost_clay_bot,
blueprint.cost_obs_bot[1], blueprint.cost_geode_bot[1])
max_required_clay::Int = blueprint.cost_obs_bot[2]
max_required_obs::Int = blueprint.cost_geode_bot[2]
while length(checking) > 0
state = pop!(checking)
for option in options
if check_resource(option, state, blueprint)
new_state = decision(option, state, blueprint)
time = new_state.time
if (max_possible_geodes(new_state, limit) > most_geodes) &&
(new_state.n_obs_bot <= max_required_obs) &&
(new_state.n_clay_bot <= max_required_clay) &&
(new_state.n_ore_bot <= max_required_ore)
if new_state.time < limit
push!(checking, new_state)
else
push!(checked, new_state)
end
most_geodes = max(most_geodes, new_state.n_geode)
end
end
end
end
return most_geodes
end
blueprints = parse_input("day19.txt")
# part 1
limit = 24
most_geodes_list = Int[0 for _ in 1:length(blueprints)]
for i in 1:length(blueprints)
blueprint = blueprints[i]
most_geodes = get_most_geodes(blueprint, limit)
most_geodes_list[i] = most_geodes
end
println("Part 1: ",sum(i * most_geodes_list[i] for i in eachindex(blueprints)))
# part 2
limit = 32
most_geodes_list = Int[0 for _ in 1:3]
for i in 1:3
blueprint = blueprints[i]
most_geodes = get_most_geodes(blueprint, limit)
most_geodes_list[i] = most_geodes
end
println("Part 2: ", prod(most_geodes_list))
println()