diff options
author | Yigit Sever | 2022-12-07 00:59:22 +0300 |
---|---|---|
committer | Yigit Sever | 2022-12-07 00:59:22 +0300 |
commit | 4388234565d34d1afc6502db34a85f424fec9a91 (patch) | |
tree | 96ea12b11f68d02687920c26fa421b4c814daae2 /2022/day5/part1/src | |
parent | 244d3dfa233371db0adcff329283bb1ce9bba9f5 (diff) | |
download | aoc-4388234565d34d1afc6502db34a85f424fec9a91.tar.gz aoc-4388234565d34d1afc6502db34a85f424fec9a91.tar.bz2 aoc-4388234565d34d1afc6502db34a85f424fec9a91.zip |
2022, day5: part1
Diffstat (limited to '2022/day5/part1/src')
-rw-r--r-- | 2022/day5/part1/src/main.rs | 62 |
1 files changed, 62 insertions, 0 deletions
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 @@ | |||
1 | use itertools::Itertools; | ||
2 | use regex::Regex; | ||
3 | use std::collections::LinkedList; | ||
4 | |||
5 | fn 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 | } | ||