blob: f64fccec9d4f7573fe2968ac94afabab93d94ad5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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<LinkedList<char>> = 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::<String>()
);
}
|