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(); let mut temp: Vec = Vec::new(); for _ in 0..how_many { if let Some(box_) = crate_builder[from - 1].pop_front() { temp.push(box_); } } temp.reverse(); for box_ in temp.iter() { crate_builder[to - 1].push_front(*box_); } } } println!( "{}", crate_builder .iter() .filter_map(|s| s.front()) .collect::() ); }