summaryrefslogtreecommitdiffstats
path: root/src/bin/2022/day19.rs
blob: c282cc2183f9278145e9c019889d3ae71a9a322e (plain) (blame)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#![allow(dead_code)]
#![allow(unused_variables)]

use std::hash::{Hash, Hasher};

use advent_of_code::prelude::*;
use ahash::AHasher;

#[derive(Clone, Hash)]
pub struct Resources {
    ore: u64,
    clay: u64,
    obsidian: u64,
    geode: u64,
}

impl Resources {
    pub fn new(ore: u64, clay: u64, obsidian: u64) -> Self {
        Self {
            ore,
            clay,
            obsidian,
            geode: 0,
        }
    }

    fn can_build(&self, blueprint: &Resources) -> Option<Self> {
        let Some(ore) = self.ore.checked_sub(blueprint.ore)
        else { return None };
        let Some(clay) = self.clay.checked_sub(blueprint.clay)
        else { return None };
        let Some(obsidian) = self.obsidian.checked_sub(blueprint.obsidian)
        else { return None };
        let Some(geode) = self.geode.checked_sub(blueprint.geode)
        else { return None };
        Some(Self {
            ore,
            clay,
            obsidian,
            geode,
        })
    }
}

#[derive(Clone, Hash)]
struct Pack {
    resources: Resources,

    ore_robots: u64,
    clay_robots: u64,
    obsidian_robots: u64,
    geode_robots: u64,
}

impl Pack {
    fn gather(&mut self) {
        self.resources.ore += self.ore_robots;
        self.resources.clay += self.clay_robots;
        self.resources.obsidian += self.obsidian_robots;
        self.resources.geode += self.geode_robots;
    }

    fn build_ore_robot(&self, blueprint: &Blueprint) -> Option<Self> {
        self.resources.can_build(&blueprint.ore).map(|resources| {
            let mut pack = self.clone();
            pack.resources = resources;
            pack
        })
    }

    fn build_clay_robot(&self, blueprint: &Blueprint) -> Option<Self> {
        self.resources.can_build(&blueprint.clay).map(|resources| {
            let mut pack = self.clone();
            pack.resources = resources;
            pack
        })
    }

    fn build_obsidian_robot(&self, blueprint: &Blueprint) -> Option<Self> {
        self.resources
            .can_build(&blueprint.obsidian)
            .map(|resources| {
                let mut pack = self.clone();
                pack.resources = resources;
                pack
            })
    }

    fn build_geode_robot(&self, blueprint: &Blueprint) -> Option<Self> {
        self.resources.can_build(&blueprint.geode).map(|resources| {
            let mut pack = self.clone();
            pack.resources = resources;
            pack
        })
    }
}

impl Default for Pack {
    fn default() -> Self {
        Self {
            resources: Resources {
                ore: 0,
                clay: 0,
                obsidian: 0,
                geode: 0,
            },

            ore_robots: 1,
            clay_robots: 0,
            obsidian_robots: 0,
            geode_robots: 0,
        }
    }
}

pub struct Blueprint {
    ore: Resources,
    clay: Resources,
    obsidian: Resources,
    geode: Resources,
}

impl Blueprint {
    fn max_geodes(&self, max_time: u64) -> u64 {
        let mut memo = HashMap::new();
        self.max_geodes_rec_memoized(
            &mut memo,
            Pack::default(),
            0,
            max_time,
            false,
            false,
            false,
        )
    }

    #[allow(clippy::too_many_arguments)]
    fn max_geodes_rec_memoized(
        &self,
        memo: &mut HashMap<u64, u64>,
        pack: Pack,
        time: u64,
        max_time: u64,
        skipped_ore: bool,
        skipped_clay: bool,
        skipped_obsidian: bool,
    ) -> u64 {
        let mut hasher = AHasher::default();
        pack.hash(&mut hasher);
        time.hash(&mut hasher);
        let hash = hasher.finish();
        if let Some(max) = memo.get(&hash) {
            return *max;
        }
        let max = self.max_geodes_rec(
            memo,
            pack,
            time,
            max_time,
            skipped_ore,
            skipped_clay,
            skipped_obsidian,
        );
        memo.insert(hash, max);
        max
    }

    #[allow(clippy::too_many_arguments)]
    fn max_geodes_rec(
        &self,
        memo: &mut HashMap<u64, u64>,
        mut pack: Pack,
        time: u64,
        max_time: u64,
        skipped_ore: bool,
        skipped_clay: bool,
        skipped_obsidian: bool,
    ) -> u64 {
        if time >= max_time {
            return pack.resources.geode;
        }

        if let Some(mut pack) = pack.build_geode_robot(self) {
            pack.gather();
            pack.geode_robots += 1;
            return self.max_geodes_rec_memoized(
                memo,
                pack,
                time + 1,
                max_time,
                false,
                false,
                false,
            );
        }

        let mut max = 0;

        let mut can_build_ore = false;
        let mut can_build_clay = false;
        let mut can_build_obsidian = false;
        if skipped_ore {
            can_build_ore = true;
        } else if let Some(mut pack) = pack.build_ore_robot(self) {
            pack.gather();
            pack.ore_robots += 1;
            let next = self.max_geodes_rec_memoized(
                memo,
                pack,
                time + 1,
                max_time,
                false,
                false,
                false,
            );
            if next > max {
                max = next;
            }
            can_build_ore = true;
        }
        if skipped_clay {
            can_build_clay = true;
        } else if let Some(mut pack) = pack.build_clay_robot(self) {
            pack.gather();
            pack.clay_robots += 1;
            let next = self.max_geodes_rec_memoized(
                memo,
                pack,
                time + 1,
                max_time,
                false,
                false,
                false,
            );
            if next > max {
                max = next;
            }
            can_build_clay = true;
        }
        if skipped_obsidian {
            can_build_obsidian = true;
        } else if let Some(mut pack) = pack.build_obsidian_robot(self) {
            pack.gather();
            pack.obsidian_robots += 1;
            let next = self.max_geodes_rec_memoized(
                memo,
                pack,
                time + 1,
                max_time,
                false,
                false,
                false,
            );
            if next > max {
                max = next;
            }
            can_build_obsidian = true;
        }

        pack.gather();
        let next = self.max_geodes_rec_memoized(
            memo,
            pack,
            time + 1,
            max_time,
            can_build_ore,
            can_build_clay,
            can_build_obsidian,
        );
        if next > max {
            max = next;
        }

        max
    }
}

impl std::str::FromStr for Blueprint {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let cap = regex_captures!(r"Blueprint \d+: Each ore robot costs (\d+) ore. Each clay robot costs (\d+) ore. Each obsidian robot costs (\d+) ore and (\d+) clay. Each geode robot costs (\d+) ore and (\d+) obsidian.", s).unwrap();
        let ore = cap[1].parse()?;
        let clay = cap[2].parse()?;
        let obsidian_ore = cap[3].parse()?;
        let obsidian_clay = cap[4].parse()?;
        let geode_ore = cap[5].parse()?;
        let geode_obsidian = cap[6].parse()?;
        Ok(Blueprint {
            ore: Resources::new(ore, 0, 0),
            clay: Resources::new(clay, 0, 0),
            obsidian: Resources::new(obsidian_ore, obsidian_clay, 0),
            geode: Resources::new(geode_ore, 0, geode_obsidian),
        })
    }
}

pub fn parse(fh: File) -> Result<impl Iterator<Item = Blueprint>> {
    Ok(parse::raw_lines(fh).map(|s| s.parse().unwrap()))
}

pub fn part1(blueprints: impl Iterator<Item = Blueprint>) -> Result<u64> {
    let mut total = 0;
    for (i, blueprint) in blueprints.enumerate() {
        total += blueprint.max_geodes(24) * (i as u64 + 1);
    }
    Ok(total)
}

pub fn part2(mut blueprints: impl Iterator<Item = Blueprint>) -> Result<u64> {
    Ok(blueprints.by_ref().next().unwrap().max_geodes(32)
        * blueprints.by_ref().next().unwrap().max_geodes(32)
        * blueprints.by_ref().next().unwrap().max_geodes(32))
}

#[test]
fn test() {
    assert_eq!(
        part1(parse(parse::data(2022, 19).unwrap()).unwrap()).unwrap(),
        1147
    );
    // takes a long time to run
    if false {
        assert_eq!(
            part2(parse(parse::data(2022, 19).unwrap()).unwrap()).unwrap(),
            3080
        );
    }
}