diff options
author | Yigit Sever | 2022-12-07 01:06:21 +0300 |
---|---|---|
committer | Yigit Sever | 2022-12-07 01:06:21 +0300 |
commit | 066f5946e25061d23db5f988061b316eeb043faf (patch) | |
tree | 0cb0e1c150b89e224307ab94ca37fc1ddac0e0e0 /2022/day5/part2 | |
parent | 4388234565d34d1afc6502db34a85f424fec9a91 (diff) | |
download | aoc-main.tar.gz aoc-main.tar.bz2 aoc-main.zip |
2022, day5: part 2donemain
This felt bad, go over this later
Diffstat (limited to '2022/day5/part2')
-rw-r--r-- | 2022/day5/part2/Cargo.toml | 10 | ||||
-rw-r--r-- | 2022/day5/part2/src/main.rs | 69 |
2 files changed, 79 insertions, 0 deletions
diff --git a/2022/day5/part2/Cargo.toml b/2022/day5/part2/Cargo.toml new file mode 100644 index 0000000..57ffa72 --- /dev/null +++ b/2022/day5/part2/Cargo.toml | |||
@@ -0,0 +1,10 @@ | |||
1 | [package] | ||
2 | name = "part2" | ||
3 | version = "0.1.0" | ||
4 | edition = "2021" | ||
5 | |||
6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
7 | |||
8 | [dependencies] | ||
9 | itertools = "0.10.5" | ||
10 | regex = "1.7.0" | ||
diff --git a/2022/day5/part2/src/main.rs b/2022/day5/part2/src/main.rs new file mode 100644 index 0000000..c21f270 --- /dev/null +++ b/2022/day5/part2/src/main.rs | |||
@@ -0,0 +1,69 @@ | |||
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 | let mut temp: Vec<char> = Vec::new(); | ||
48 | for _ in 0..how_many { | ||
49 | if let Some(box_) = crate_builder[from - 1].pop_front() { | ||
50 | temp.push(box_); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | temp.reverse(); | ||
55 | |||
56 | for box_ in temp.iter() { | ||
57 | crate_builder[to - 1].push_front(*box_); | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | |||
62 | println!( | ||
63 | "{}", | ||
64 | crate_builder | ||
65 | .iter() | ||
66 | .filter_map(|s| s.front()) | ||
67 | .collect::<String>() | ||
68 | ); | ||
69 | } | ||