|
@@ -0,0 +1,298 @@
|
|
1
|
+#[macro_use]
|
|
2
|
+extern crate lazy_static;
|
|
3
|
+
|
|
4
|
+use std::collections::HashMap;
|
|
5
|
+use std::error::Error;
|
|
6
|
+use std::fs::read_to_string;
|
|
7
|
+use std::result;
|
|
8
|
+use std::str::FromStr;
|
|
9
|
+
|
|
10
|
+use regex::Regex;
|
|
11
|
+
|
|
12
|
+type Result<T> = result::Result<T, Box<dyn Error>>;
|
|
13
|
+
|
|
14
|
+const INPUT: &str = "input/input.txt";
|
|
15
|
+
|
|
16
|
+#[derive(Debug, PartialEq)]
|
|
17
|
+struct Reactions {
|
|
18
|
+ reactions: HashMap<String, Reaction>,
|
|
19
|
+}
|
|
20
|
+
|
|
21
|
+#[derive(Debug, PartialEq)]
|
|
22
|
+struct Reaction {
|
|
23
|
+ output: ChemicalAmount,
|
|
24
|
+ inputs: Vec<ChemicalAmount>,
|
|
25
|
+}
|
|
26
|
+
|
|
27
|
+#[derive(Debug, Hash, PartialEq, Eq)]
|
|
28
|
+struct ChemicalAmount {
|
|
29
|
+ chemical: String,
|
|
30
|
+ amount: u32,
|
|
31
|
+}
|
|
32
|
+
|
|
33
|
+impl FromStr for Reactions {
|
|
34
|
+ type Err = Box<dyn Error>;
|
|
35
|
+
|
|
36
|
+ fn from_str(s: &str) -> Result<Reactions> {
|
|
37
|
+ lazy_static! {
|
|
38
|
+ static ref SOURCE_CHEMICALS: Regex =
|
|
39
|
+ Regex::new(concat!(r"(?P<input_amount>\d+) (?P<input_chemical>\w+),? ",)).unwrap();
|
|
40
|
+ static ref OUTPUT_CHEMICAL: Regex = Regex::new(concat!(
|
|
41
|
+ r"=> (?P<output_amount>\d+) (?P<output_chemical>\w+)"
|
|
42
|
+ ))
|
|
43
|
+ .unwrap();
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ let mut reactions = HashMap::new();
|
|
47
|
+ for line in s.trim().split('\n') {
|
|
48
|
+ let mut inputs: Vec<ChemicalAmount> = vec![];
|
|
49
|
+ for captures in SOURCE_CHEMICALS.captures_iter(line) {
|
|
50
|
+ inputs.push(ChemicalAmount {
|
|
51
|
+ chemical: captures["input_chemical"].to_string(),
|
|
52
|
+ amount: captures["input_amount"].parse()?,
|
|
53
|
+ });
|
|
54
|
+ }
|
|
55
|
+ match OUTPUT_CHEMICAL.captures(line) {
|
|
56
|
+ None => {
|
|
57
|
+ return Err(From::from(
|
|
58
|
+ "Malformed reactions, no output chemical could be found",
|
|
59
|
+ ));
|
|
60
|
+ }
|
|
61
|
+ Some(captures) => {
|
|
62
|
+ let output = ChemicalAmount {
|
|
63
|
+ chemical: captures["output_chemical"].to_string(),
|
|
64
|
+ amount: captures["output_amount"].parse()?,
|
|
65
|
+ };
|
|
66
|
+ reactions.insert(output.chemical.clone(), Reaction { inputs, output });
|
|
67
|
+ }
|
|
68
|
+ };
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ Ok(Reactions { reactions })
|
|
72
|
+ }
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+impl Reactions {
|
|
76
|
+ fn new() -> Reactions {
|
|
77
|
+ Reactions {
|
|
78
|
+ reactions: HashMap::new(),
|
|
79
|
+ }
|
|
80
|
+ }
|
|
81
|
+}
|
|
82
|
+
|
|
83
|
+fn calculate_ore_required(
|
|
84
|
+ reactions: &Reactions,
|
|
85
|
+ produced_chemical: &ChemicalAmount,
|
|
86
|
+ left_overs: &mut HashMap<String, u32>,
|
|
87
|
+) -> u32 {
|
|
88
|
+ let reaction = &reactions.reactions[&produced_chemical.chemical];
|
|
89
|
+ let mut needed_amount = produced_chemical.amount;
|
|
90
|
+ let mut left_over = 0;
|
|
91
|
+ if let Some(left_over_amount) = left_overs.get(&produced_chemical.chemical) {
|
|
92
|
+ left_over = *left_over_amount;
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ if left_over > 0 {
|
|
96
|
+ if left_over >= needed_amount {
|
|
97
|
+ left_overs.insert(
|
|
98
|
+ produced_chemical.chemical.clone(),
|
|
99
|
+ left_over - needed_amount,
|
|
100
|
+ );
|
|
101
|
+ return 0;
|
|
102
|
+ } else {
|
|
103
|
+ left_overs.insert(produced_chemical.chemical.clone(), 0);
|
|
104
|
+ needed_amount -= left_over;
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ let ratio: f32 = needed_amount as f32 / reaction.output.amount as f32;
|
|
109
|
+ let production_count = ratio.ceil() as u32;
|
|
110
|
+ left_overs.insert(
|
|
111
|
+ produced_chemical.chemical.clone(),
|
|
112
|
+ (reaction.output.amount * production_count) - needed_amount,
|
|
113
|
+ );
|
|
114
|
+
|
|
115
|
+ if reaction.inputs.len() == 1 && reaction.inputs[0].chemical == "ORE" {
|
|
116
|
+ return reaction.inputs[0].amount * production_count;
|
|
117
|
+ } else {
|
|
118
|
+ return reaction
|
|
119
|
+ .inputs
|
|
120
|
+ .iter()
|
|
121
|
+ .map(|input| {
|
|
122
|
+ calculate_ore_required(
|
|
123
|
+ reactions,
|
|
124
|
+ &ChemicalAmount {
|
|
125
|
+ chemical: input.chemical.clone(),
|
|
126
|
+ amount: input.amount * production_count,
|
|
127
|
+ },
|
|
128
|
+ left_overs,
|
|
129
|
+ )
|
|
130
|
+ })
|
|
131
|
+ .sum();
|
|
132
|
+ }
|
|
133
|
+}
|
|
134
|
+
|
|
135
|
+fn read_reactions(filename: &str) -> Result<Reactions> {
|
|
136
|
+ let reactions = read_to_string(filename)?.parse()?;
|
|
137
|
+ Ok(reactions)
|
|
138
|
+}
|
|
139
|
+
|
|
140
|
+fn solve_part1(filename: &str) -> Result<u32> {
|
|
141
|
+ let reactions = read_reactions(filename)?;
|
|
142
|
+ let mut left_overs = HashMap::new();
|
|
143
|
+ Ok(calculate_ore_required(
|
|
144
|
+ &reactions,
|
|
145
|
+ &ChemicalAmount {
|
|
146
|
+ chemical: "FUEL".to_string(),
|
|
147
|
+ amount: 1,
|
|
148
|
+ },
|
|
149
|
+ &mut left_overs,
|
|
150
|
+ ))
|
|
151
|
+}
|
|
152
|
+
|
|
153
|
+fn solve_part2(filename: &str) -> Result<u64> {
|
|
154
|
+ Ok(1)
|
|
155
|
+}
|
|
156
|
+
|
|
157
|
+fn main() -> Result<()> {
|
|
158
|
+ println!("Part 1: {}", solve_part1(INPUT)?);
|
|
159
|
+ println!("Part 2: {}", solve_part2(INPUT)?);
|
|
160
|
+
|
|
161
|
+ Ok(())
|
|
162
|
+}
|
|
163
|
+
|
|
164
|
+#[cfg(test)]
|
|
165
|
+mod tests {
|
|
166
|
+ use super::*;
|
|
167
|
+
|
|
168
|
+ const TEST_INPUT1: &str = "input/test1.txt";
|
|
169
|
+ const TEST_INPUT2: &str = "input/test2.txt";
|
|
170
|
+ const TEST_INPUT3: &str = "input/test3.txt";
|
|
171
|
+ const TEST_INPUT4: &str = "input/test4.txt";
|
|
172
|
+ const TEST_INPUT5: &str = "input/test5.txt";
|
|
173
|
+
|
|
174
|
+ fn reactions_1() -> Reactions {
|
|
175
|
+ Reactions {
|
|
176
|
+ reactions: vec![
|
|
177
|
+ (
|
|
178
|
+ "E".to_string(),
|
|
179
|
+ Reaction {
|
|
180
|
+ output: ChemicalAmount {
|
|
181
|
+ chemical: "E".to_string(),
|
|
182
|
+ amount: 1,
|
|
183
|
+ },
|
|
184
|
+ inputs: vec![
|
|
185
|
+ ChemicalAmount {
|
|
186
|
+ chemical: "A".to_string(),
|
|
187
|
+ amount: 7,
|
|
188
|
+ },
|
|
189
|
+ ChemicalAmount {
|
|
190
|
+ chemical: "D".to_string(),
|
|
191
|
+ amount: 1,
|
|
192
|
+ },
|
|
193
|
+ ],
|
|
194
|
+ },
|
|
195
|
+ ),
|
|
196
|
+ (
|
|
197
|
+ "A".to_string(),
|
|
198
|
+ Reaction {
|
|
199
|
+ output: ChemicalAmount {
|
|
200
|
+ chemical: "A".to_string(),
|
|
201
|
+ amount: 10,
|
|
202
|
+ },
|
|
203
|
+ inputs: vec![ChemicalAmount {
|
|
204
|
+ chemical: "ORE".to_string(),
|
|
205
|
+ amount: 10,
|
|
206
|
+ }],
|
|
207
|
+ },
|
|
208
|
+ ),
|
|
209
|
+ (
|
|
210
|
+ "D".to_string(),
|
|
211
|
+ Reaction {
|
|
212
|
+ output: ChemicalAmount {
|
|
213
|
+ chemical: "D".to_string(),
|
|
214
|
+ amount: 1,
|
|
215
|
+ },
|
|
216
|
+ inputs: vec![
|
|
217
|
+ ChemicalAmount {
|
|
218
|
+ chemical: "A".to_string(),
|
|
219
|
+ amount: 7,
|
|
220
|
+ },
|
|
221
|
+ ChemicalAmount {
|
|
222
|
+ chemical: "C".to_string(),
|
|
223
|
+ amount: 1,
|
|
224
|
+ },
|
|
225
|
+ ],
|
|
226
|
+ },
|
|
227
|
+ ),
|
|
228
|
+ (
|
|
229
|
+ "FUEL".to_string(),
|
|
230
|
+ Reaction {
|
|
231
|
+ output: ChemicalAmount {
|
|
232
|
+ chemical: "FUEL".to_string(),
|
|
233
|
+ amount: 1,
|
|
234
|
+ },
|
|
235
|
+ inputs: vec![
|
|
236
|
+ ChemicalAmount {
|
|
237
|
+ chemical: "A".to_string(),
|
|
238
|
+ amount: 7,
|
|
239
|
+ },
|
|
240
|
+ ChemicalAmount {
|
|
241
|
+ chemical: "E".to_string(),
|
|
242
|
+ amount: 1,
|
|
243
|
+ },
|
|
244
|
+ ],
|
|
245
|
+ },
|
|
246
|
+ ),
|
|
247
|
+ (
|
|
248
|
+ "B".to_string(),
|
|
249
|
+ Reaction {
|
|
250
|
+ output: ChemicalAmount {
|
|
251
|
+ chemical: "B".to_string(),
|
|
252
|
+ amount: 1,
|
|
253
|
+ },
|
|
254
|
+ inputs: vec![ChemicalAmount {
|
|
255
|
+ chemical: "ORE".to_string(),
|
|
256
|
+ amount: 1,
|
|
257
|
+ }],
|
|
258
|
+ },
|
|
259
|
+ ),
|
|
260
|
+ (
|
|
261
|
+ "C".to_string(),
|
|
262
|
+ Reaction {
|
|
263
|
+ output: ChemicalAmount {
|
|
264
|
+ chemical: "C".to_string(),
|
|
265
|
+ amount: 1,
|
|
266
|
+ },
|
|
267
|
+ inputs: vec![
|
|
268
|
+ ChemicalAmount {
|
|
269
|
+ chemical: "A".to_string(),
|
|
270
|
+ amount: 7,
|
|
271
|
+ },
|
|
272
|
+ ChemicalAmount {
|
|
273
|
+ chemical: "B".to_string(),
|
|
274
|
+ amount: 1,
|
|
275
|
+ },
|
|
276
|
+ ],
|
|
277
|
+ },
|
|
278
|
+ ),
|
|
279
|
+ ]
|
|
280
|
+ .into_iter()
|
|
281
|
+ .collect(),
|
|
282
|
+ }
|
|
283
|
+ }
|
|
284
|
+
|
|
285
|
+ #[test]
|
|
286
|
+ fn reads_reactions() {
|
|
287
|
+ assert_eq!(read_reactions(TEST_INPUT1).unwrap(), reactions_1());
|
|
288
|
+ }
|
|
289
|
+
|
|
290
|
+ #[test]
|
|
291
|
+ fn solves_part1() {
|
|
292
|
+ assert_eq!(solve_part1(TEST_INPUT1).unwrap(), 31);
|
|
293
|
+ assert_eq!(solve_part1(TEST_INPUT2).unwrap(), 165);
|
|
294
|
+ assert_eq!(solve_part1(TEST_INPUT3).unwrap(), 13312);
|
|
295
|
+ assert_eq!(solve_part1(TEST_INPUT4).unwrap(), 180697);
|
|
296
|
+ assert_eq!(solve_part1(TEST_INPUT5).unwrap(), 2210736);
|
|
297
|
+ }
|
|
298
|
+}
|