From 4388234565d34d1afc6502db34a85f424fec9a91 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Wed, 7 Dec 2022 00:59:22 +0300 Subject: 2022, day5: part1 --- 2022/day5/part1/src/main.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 2022/day5/part1/src/main.rs (limited to '2022/day5/part1/src') 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 @@ +use itertools::Itertools; +use regex::Regex; +use std::collections::LinkedList; + +fn main() { + let input: &str = include_str!("../../input"); + + let width_finder = Regex::new(r"(?m)^\s1.*(\d)\s$").unwrap(); + let mut width = 0; + + for cap in width_finder.captures_iter(input) { + width = str::parse(&cap[1]).unwrap(); + } + + let mut crate_builder: Vec> = Vec::new(); + + for _ in 0..width { + crate_builder.push(LinkedList::new()); + } + + for line in input.lines() { + let mut idx = 0; + if line.contains("1") { + break; + } + + for part in &line.chars().chunks(4) { + let crt: String = part.collect(); + if !crt.contains("[") { + idx += 1; + } else { + let crate_ = crt.chars().nth(1).unwrap(); + crate_builder[idx].push_back(crate_); + idx += 1; + } + } + } + + let move_parser = Regex::new(r"^move\s(\d+)\sfrom\s(\d)\sto\s(\d)").unwrap(); + + for mv in input.lines().skip_while(|l| !l.contains("move")) { + for cap in move_parser.captures_iter(mv) { + let how_many = str::parse(&cap[1]).unwrap(); + let from: usize = str::parse(&cap[2]).unwrap(); + let to: usize = str::parse(&cap[3]).unwrap(); + + for _ in 0..how_many { + if let Some(box_) = crate_builder[from - 1].pop_front() { + crate_builder[to - 1].push_front(box_); + } + } + } + } + + println!( + "{}", + crate_builder + .iter() + .filter_map(|s| s.front()) + .collect::() + ); +} -- cgit v1.2.3-70-g09d2