summaryrefslogtreecommitdiffstats
path: root/2022
diff options
context:
space:
mode:
Diffstat (limited to '2022')
-rw-r--r--2022/day5/example9
-rw-r--r--2022/day5/input512
-rw-r--r--2022/day5/part1/Cargo.toml10
-rw-r--r--2022/day5/part1/src/main.rs62
4 files changed, 593 insertions, 0 deletions
diff --git a/2022/day5/example b/2022/day5/example
new file mode 100644
index 0000000..42ef47f
--- /dev/null
+++ b/2022/day5/example
@@ -0,0 +1,9 @@
1 [D]
2[N] [C]
3[Z] [M] [P]
4 1 2 3
5
6move 1 from 2 to 1
7move 3 from 1 to 3
8move 2 from 2 to 1
9move 1 from 1 to 2
diff --git a/2022/day5/input b/2022/day5/input
new file mode 100644
index 0000000..76316f1
--- /dev/null
+++ b/2022/day5/input
@@ -0,0 +1,512 @@
1[J] [F] [M]
2[Z] [F] [G] [Q] [F]
3[G] [P] [H] [Z] [S] [Q]
4[V] [W] [Z] [P] [D] [G] [P]
5[T] [D] [S] [Z] [N] [W] [B] [N]
6[D] [M] [R] [J] [J] [P] [V] [P] [J]
7[B] [R] [C] [T] [C] [V] [C] [B] [P]
8[N] [S] [V] [R] [T] [N] [G] [Z] [W]
9 1 2 3 4 5 6 7 8 9
10
11move 2 from 4 to 6
12move 1 from 9 to 5
13move 3 from 2 to 4
14move 8 from 4 to 7
15move 2 from 9 to 7
16move 3 from 8 to 3
17move 2 from 1 to 2
18move 5 from 7 to 9
19move 1 from 9 to 4
20move 1 from 8 to 3
21move 1 from 3 to 4
22move 2 from 4 to 9
23move 7 from 3 to 5
24move 6 from 1 to 8
25move 11 from 7 to 9
26move 12 from 5 to 3
27move 6 from 6 to 9
28move 3 from 3 to 8
29move 4 from 2 to 7
30move 3 from 5 to 7
31move 1 from 5 to 7
32move 2 from 2 to 5
33move 1 from 5 to 2
34move 5 from 8 to 9
35move 7 from 7 to 2
36move 3 from 8 to 7
37move 1 from 8 to 9
38move 4 from 3 to 6
39move 1 from 5 to 1
40move 9 from 9 to 6
41move 7 from 9 to 6
42move 20 from 6 to 5
43move 12 from 9 to 8
44move 5 from 5 to 1
45move 3 from 7 to 4
46move 6 from 2 to 7
47move 2 from 3 to 1
48move 4 from 3 to 8
49move 1 from 4 to 1
50move 7 from 7 to 5
51move 4 from 8 to 2
52move 3 from 6 to 2
53move 3 from 2 to 9
54move 4 from 1 to 7
55move 2 from 1 to 2
56move 3 from 9 to 5
57move 11 from 8 to 5
58move 1 from 6 to 9
59move 1 from 8 to 5
60move 1 from 1 to 2
61move 24 from 5 to 4
62move 2 from 1 to 6
63move 11 from 5 to 4
64move 2 from 7 to 9
65move 1 from 6 to 2
66move 4 from 2 to 1
67move 28 from 4 to 2
68move 1 from 7 to 8
69move 9 from 2 to 5
70move 2 from 9 to 6
71move 4 from 4 to 2
72move 1 from 7 to 4
73move 3 from 4 to 7
74move 1 from 6 to 9
75move 21 from 2 to 3
76move 3 from 1 to 6
77move 5 from 6 to 2
78move 7 from 2 to 3
79move 1 from 9 to 3
80move 1 from 8 to 4
81move 1 from 7 to 8
82move 3 from 5 to 8
83move 1 from 1 to 7
84move 2 from 7 to 9
85move 2 from 8 to 4
86move 1 from 9 to 2
87move 1 from 8 to 6
88move 11 from 3 to 4
89move 1 from 7 to 8
90move 6 from 5 to 9
91move 2 from 8 to 7
92move 1 from 6 to 5
93move 7 from 3 to 8
94move 9 from 3 to 6
95move 1 from 8 to 3
96move 1 from 7 to 4
97move 2 from 3 to 5
98move 4 from 5 to 7
99move 4 from 6 to 8
100move 2 from 7 to 9
101move 11 from 4 to 2
102move 1 from 4 to 2
103move 6 from 8 to 9
104move 1 from 7 to 1
105move 1 from 3 to 7
106move 3 from 7 to 8
107move 6 from 8 to 9
108move 6 from 4 to 8
109move 18 from 9 to 3
110move 1 from 5 to 8
111move 5 from 6 to 5
112move 6 from 8 to 1
113move 3 from 5 to 4
114move 1 from 9 to 8
115move 3 from 4 to 8
116move 15 from 3 to 6
117move 2 from 5 to 9
118move 3 from 3 to 1
119move 9 from 6 to 4
120move 2 from 1 to 5
121move 2 from 5 to 8
122move 6 from 4 to 2
123move 6 from 1 to 6
124move 3 from 4 to 6
125move 6 from 9 to 1
126move 4 from 2 to 1
127move 7 from 8 to 1
128move 1 from 6 to 7
129move 17 from 1 to 5
130move 1 from 7 to 1
131move 5 from 2 to 1
132move 1 from 8 to 6
133move 11 from 6 to 4
134move 2 from 2 to 3
135move 3 from 1 to 8
136move 7 from 2 to 5
137move 4 from 6 to 7
138move 4 from 1 to 5
139move 15 from 5 to 9
140move 2 from 3 to 7
141move 2 from 8 to 2
142move 1 from 1 to 9
143move 6 from 2 to 6
144move 7 from 5 to 6
145move 5 from 7 to 3
146move 1 from 6 to 1
147move 2 from 3 to 4
148move 1 from 3 to 4
149move 5 from 6 to 4
150move 14 from 9 to 2
151move 1 from 8 to 9
152move 1 from 7 to 8
153move 1 from 9 to 6
154move 2 from 9 to 5
155move 1 from 1 to 2
156move 7 from 6 to 9
157move 1 from 3 to 4
158move 8 from 5 to 2
159move 1 from 6 to 7
160move 1 from 7 to 4
161move 1 from 8 to 4
162move 1 from 3 to 9
163move 7 from 9 to 5
164move 1 from 9 to 1
165move 6 from 5 to 1
166move 8 from 2 to 4
167move 1 from 5 to 6
168move 1 from 6 to 7
169move 1 from 7 to 9
170move 7 from 2 to 9
171move 1 from 9 to 4
172move 3 from 9 to 1
173move 1 from 9 to 6
174move 11 from 2 to 8
175move 9 from 1 to 8
176move 1 from 6 to 4
177move 1 from 1 to 9
178move 12 from 4 to 2
179move 4 from 9 to 3
180move 3 from 4 to 6
181move 9 from 8 to 6
182move 12 from 4 to 9
183move 8 from 6 to 3
184move 8 from 2 to 7
185move 11 from 3 to 4
186move 2 from 2 to 7
187move 2 from 6 to 1
188move 1 from 2 to 3
189move 2 from 6 to 2
190move 3 from 2 to 6
191move 2 from 1 to 6
192move 1 from 6 to 1
193move 1 from 6 to 4
194move 2 from 6 to 3
195move 1 from 6 to 5
196move 4 from 3 to 8
197move 12 from 4 to 5
198move 5 from 9 to 7
199move 3 from 8 to 7
200move 1 from 9 to 1
201move 3 from 8 to 2
202move 13 from 5 to 6
203move 1 from 2 to 9
204move 13 from 6 to 7
205move 7 from 9 to 6
206move 2 from 4 to 6
207move 1 from 8 to 6
208move 1 from 1 to 6
209move 1 from 2 to 9
210move 1 from 2 to 3
211move 12 from 7 to 9
212move 7 from 8 to 4
213move 1 from 1 to 3
214move 2 from 7 to 9
215move 15 from 7 to 4
216move 8 from 6 to 3
217move 1 from 8 to 9
218move 1 from 7 to 2
219move 10 from 3 to 5
220move 6 from 5 to 9
221move 1 from 2 to 8
222move 1 from 5 to 8
223move 2 from 8 to 9
224move 10 from 4 to 9
225move 20 from 9 to 6
226move 1 from 7 to 6
227move 4 from 9 to 3
228move 1 from 5 to 9
229move 4 from 4 to 9
230move 8 from 9 to 7
231move 2 from 5 to 1
232move 7 from 4 to 3
233move 8 from 3 to 2
234move 6 from 9 to 8
235move 1 from 3 to 7
236move 1 from 3 to 1
237move 7 from 7 to 8
238move 13 from 8 to 3
239move 2 from 2 to 8
240move 1 from 8 to 2
241move 1 from 4 to 1
242move 1 from 1 to 8
243move 2 from 8 to 2
244move 24 from 6 to 2
245move 2 from 7 to 8
246move 5 from 3 to 4
247move 25 from 2 to 6
248move 5 from 4 to 9
249move 2 from 8 to 7
250move 2 from 7 to 3
251move 4 from 6 to 2
252move 2 from 6 to 4
253move 9 from 2 to 3
254move 11 from 3 to 7
255move 10 from 7 to 8
256move 1 from 7 to 9
257move 3 from 2 to 4
258move 8 from 8 to 2
259move 1 from 2 to 6
260move 2 from 4 to 1
261move 1 from 8 to 2
262move 1 from 6 to 9
263move 1 from 8 to 3
264move 6 from 9 to 7
265move 2 from 9 to 1
266move 9 from 6 to 8
267move 7 from 2 to 3
268move 7 from 8 to 2
269move 10 from 6 to 8
270move 7 from 1 to 2
271move 9 from 3 to 2
272move 5 from 3 to 8
273move 4 from 7 to 2
274move 2 from 3 to 2
275move 12 from 2 to 3
276move 6 from 4 to 2
277move 1 from 7 to 6
278move 5 from 3 to 5
279move 16 from 8 to 4
280move 12 from 2 to 7
281move 5 from 5 to 7
282move 1 from 8 to 3
283move 1 from 6 to 4
284move 17 from 7 to 4
285move 1 from 7 to 1
286move 1 from 1 to 9
287move 1 from 9 to 5
288move 11 from 4 to 9
289move 10 from 2 to 3
290move 1 from 5 to 4
291move 1 from 9 to 2
292move 2 from 2 to 1
293move 1 from 2 to 3
294move 23 from 4 to 5
295move 7 from 9 to 7
296move 3 from 9 to 1
297move 20 from 5 to 6
298move 3 from 5 to 8
299move 1 from 4 to 1
300move 2 from 8 to 3
301move 4 from 6 to 4
302move 7 from 7 to 2
303move 1 from 8 to 4
304move 19 from 3 to 9
305move 5 from 1 to 7
306move 7 from 2 to 6
307move 3 from 7 to 5
308move 2 from 3 to 4
309move 1 from 5 to 4
310move 1 from 1 to 4
311move 1 from 7 to 6
312move 13 from 6 to 7
313move 6 from 9 to 3
314move 1 from 3 to 5
315move 2 from 3 to 4
316move 2 from 6 to 2
317move 3 from 4 to 3
318move 8 from 9 to 1
319move 2 from 2 to 1
320move 8 from 6 to 7
321move 2 from 9 to 4
322move 20 from 7 to 1
323move 2 from 7 to 5
324move 2 from 5 to 1
325move 8 from 1 to 8
326move 8 from 8 to 6
327move 1 from 6 to 9
328move 8 from 6 to 1
329move 1 from 5 to 3
330move 7 from 3 to 2
331move 1 from 5 to 2
332move 2 from 9 to 7
333move 1 from 5 to 8
334move 18 from 1 to 4
335move 1 from 8 to 9
336move 3 from 2 to 3
337move 2 from 7 to 4
338move 5 from 2 to 4
339move 3 from 3 to 8
340move 8 from 1 to 7
341move 2 from 9 to 2
342move 32 from 4 to 5
343move 1 from 9 to 7
344move 1 from 2 to 1
345move 6 from 1 to 6
346move 1 from 2 to 4
347move 3 from 8 to 1
348move 3 from 6 to 5
349move 1 from 3 to 6
350move 2 from 1 to 9
351move 4 from 4 to 7
352move 31 from 5 to 4
353move 4 from 5 to 6
354move 1 from 6 to 1
355move 7 from 6 to 5
356move 1 from 9 to 4
357move 19 from 4 to 2
358move 1 from 5 to 9
359move 5 from 5 to 6
360move 3 from 4 to 2
361move 2 from 7 to 1
362move 4 from 7 to 8
363move 3 from 8 to 6
364move 2 from 6 to 7
365move 6 from 7 to 8
366move 3 from 1 to 5
367move 4 from 5 to 9
368move 15 from 2 to 1
369move 4 from 6 to 4
370move 2 from 6 to 3
371move 1 from 3 to 7
372move 4 from 1 to 2
373move 1 from 3 to 4
374move 2 from 7 to 4
375move 5 from 9 to 3
376move 2 from 7 to 3
377move 16 from 4 to 8
378move 8 from 8 to 5
379move 2 from 1 to 5
380move 1 from 9 to 6
381move 1 from 6 to 5
382move 7 from 5 to 9
383move 3 from 1 to 8
384move 1 from 8 to 4
385move 8 from 2 to 7
386move 3 from 1 to 3
387move 1 from 3 to 9
388move 2 from 4 to 2
389move 7 from 8 to 5
390move 7 from 9 to 1
391move 6 from 3 to 5
392move 6 from 7 to 4
393move 3 from 4 to 1
394move 3 from 2 to 5
395move 1 from 7 to 8
396move 1 from 7 to 5
397move 1 from 9 to 8
398move 2 from 2 to 4
399move 15 from 1 to 6
400move 8 from 5 to 9
401move 3 from 3 to 4
402move 4 from 4 to 3
403move 1 from 9 to 7
404move 6 from 9 to 4
405move 1 from 9 to 2
406move 6 from 4 to 9
407move 2 from 4 to 6
408move 5 from 6 to 9
409move 1 from 3 to 1
410move 8 from 6 to 8
411move 12 from 5 to 3
412move 1 from 5 to 3
413move 1 from 3 to 8
414move 4 from 6 to 1
415move 11 from 3 to 8
416move 1 from 2 to 1
417move 23 from 8 to 2
418move 3 from 1 to 2
419move 1 from 1 to 9
420move 2 from 2 to 3
421move 6 from 3 to 6
422move 1 from 7 to 6
423move 1 from 4 to 7
424move 1 from 4 to 3
425move 1 from 7 to 3
426move 4 from 8 to 4
427move 2 from 1 to 8
428move 3 from 8 to 1
429move 4 from 6 to 2
430move 7 from 9 to 1
431move 1 from 9 to 6
432move 2 from 2 to 3
433move 3 from 9 to 4
434move 1 from 9 to 3
435move 10 from 2 to 8
436move 16 from 2 to 5
437move 2 from 3 to 6
438move 6 from 1 to 8
439move 1 from 1 to 5
440move 8 from 8 to 5
441move 11 from 5 to 9
442move 2 from 1 to 8
443move 1 from 1 to 8
444move 4 from 4 to 6
445move 3 from 3 to 9
446move 14 from 9 to 3
447move 15 from 8 to 5
448move 9 from 5 to 4
449move 7 from 6 to 1
450move 1 from 6 to 3
451move 4 from 4 to 7
452move 2 from 6 to 2
453move 4 from 7 to 4
454move 4 from 1 to 4
455move 10 from 4 to 3
456move 14 from 3 to 6
457move 5 from 4 to 1
458move 6 from 5 to 7
459move 1 from 2 to 6
460move 3 from 7 to 2
461move 2 from 2 to 3
462move 3 from 7 to 8
463move 2 from 8 to 2
464move 2 from 2 to 7
465move 6 from 6 to 2
466move 1 from 8 to 7
467move 8 from 2 to 7
468move 1 from 4 to 1
469move 5 from 5 to 3
470move 3 from 3 to 2
471move 5 from 1 to 3
472move 7 from 5 to 8
473move 6 from 6 to 3
474move 1 from 5 to 9
475move 10 from 7 to 9
476move 26 from 3 to 4
477move 1 from 5 to 1
478move 6 from 8 to 2
479move 9 from 2 to 9
480move 1 from 7 to 5
481move 1 from 8 to 5
482move 2 from 6 to 2
483move 20 from 9 to 6
484move 1 from 1 to 6
485move 1 from 4 to 2
486move 1 from 5 to 8
487move 1 from 5 to 7
488move 3 from 1 to 3
489move 1 from 3 to 6
490move 12 from 4 to 8
491move 11 from 4 to 5
492move 1 from 7 to 5
493move 1 from 2 to 8
494move 1 from 1 to 8
495move 2 from 2 to 5
496move 8 from 6 to 2
497move 5 from 6 to 4
498move 2 from 5 to 3
499move 12 from 8 to 4
500move 5 from 2 to 6
501move 3 from 8 to 1
502move 11 from 6 to 8
503move 10 from 4 to 6
504move 5 from 4 to 6
505move 12 from 6 to 5
506move 22 from 5 to 6
507move 3 from 6 to 5
508move 3 from 8 to 5
509move 1 from 3 to 8
510move 4 from 8 to 1
511move 6 from 1 to 7
512move 5 from 6 to 9
diff --git a/2022/day5/part1/Cargo.toml b/2022/day5/part1/Cargo.toml
new file mode 100644
index 0000000..a479178
--- /dev/null
+++ b/2022/day5/part1/Cargo.toml
@@ -0,0 +1,10 @@
1[package]
2name = "part1"
3version = "0.1.0"
4edition = "2021"
5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8[dependencies]
9itertools = "0.10.5"
10regex = "1.7.0"
diff --git a/2022/day5/part1/src/main.rs b/2022/day5/part1/src/main.rs
new file mode 100644
index 0000000..f64fcce
--- /dev/null
+++ b/2022/day5/part1/src/main.rs
@@ -0,0 +1,62 @@
1use itertools::Itertools;
2use regex::Regex;
3use std::collections::LinkedList;
4
5fn main() {
6 let input: &str = include_str!("../../input");
7
8 let width_finder = Regex::new(r"(?m)^\s1.*(\d)\s$").unwrap();
9 let mut width = 0;
10
11 for cap in width_finder.captures_iter(input) {
12 width = str::parse(&cap[1]).unwrap();
13 }
14
15 let mut crate_builder: Vec<LinkedList<char>> = Vec::new();
16
17 for _ in 0..width {
18 crate_builder.push(LinkedList::new());
19 }
20
21 for line in input.lines() {
22 let mut idx = 0;
23 if line.contains("1") {
24 break;
25 }
26
27 for part in &line.chars().chunks(4) {
28 let crt: String = part.collect();
29 if !crt.contains("[") {
30 idx += 1;
31 } else {
32 let crate_ = crt.chars().nth(1).unwrap();
33 crate_builder[idx].push_back(crate_);
34 idx += 1;
35 }
36 }
37 }
38
39 let move_parser = Regex::new(r"^move\s(\d+)\sfrom\s(\d)\sto\s(\d)").unwrap();
40
41 for mv in input.lines().skip_while(|l| !l.contains("move")) {
42 for cap in move_parser.captures_iter(mv) {
43 let how_many = str::parse(&cap[1]).unwrap();
44 let from: usize = str::parse(&cap[2]).unwrap();
45 let to: usize = str::parse(&cap[3]).unwrap();
46
47 for _ in 0..how_many {
48 if let Some(box_) = crate_builder[from - 1].pop_front() {
49 crate_builder[to - 1].push_front(box_);
50 }
51 }
52 }
53 }
54
55 println!(
56 "{}",
57 crate_builder
58 .iter()
59 .filter_map(|s| s.front())
60 .collect::<String>()
61 );
62}