2022 day5/rust: cleanup
This commit is contained in:
parent
d75c49e8c4
commit
92a60f149e
1 changed files with 37 additions and 41 deletions
|
@ -18,14 +18,10 @@ fn main() {
|
|||
break;
|
||||
}
|
||||
|
||||
for (i, crate_) in line.as_bytes().chunks(4).enumerate() {
|
||||
if stacks.len() <= i {
|
||||
stacks.push(vec![]);
|
||||
}
|
||||
|
||||
let crates = line.as_bytes().chunks(4).map(|crate_| {
|
||||
if crate_.iter().all(|&c| c == b' ') {
|
||||
// empty space
|
||||
continue;
|
||||
return None;
|
||||
}
|
||||
|
||||
let crate_ = crate_.strip_suffix(b" ").unwrap_or(crate_);
|
||||
|
@ -37,20 +33,43 @@ fn main() {
|
|||
panic!("invalid crate {:?} {}", crate_, std::str::from_utf8(crate_).unwrap());
|
||||
};
|
||||
|
||||
stacks[i].push(*crate_ as char);
|
||||
}
|
||||
Some(*crate_ as char)
|
||||
});
|
||||
|
||||
for (i, crate_) in crates.enumerate() {
|
||||
if stacks.len() <= i {
|
||||
stacks.push(vec![]);
|
||||
}
|
||||
|
||||
if let Some(crate_) = crate_ {
|
||||
stacks[i].push(crate_);
|
||||
}
|
||||
}
|
||||
}
|
||||
for stack in &mut stacks {
|
||||
stack.reverse();
|
||||
}
|
||||
|
||||
let stacks = stacks;
|
||||
|
||||
let mut stacks_9000 = stacks.clone();
|
||||
let mut stacks_9001 = stacks;
|
||||
|
||||
let regex = Regex::new(r"move (\d+) from (\d+) to (\d+)").unwrap();
|
||||
for line in lines {
|
||||
fn get_stacks(
|
||||
stacks: &mut [Vec<char>],
|
||||
source: usize,
|
||||
dest: usize,
|
||||
) -> (&mut Vec<char>, &mut Vec<char>) {
|
||||
let (p1, p2) = stacks.split_at_mut(usize::max(source, dest));
|
||||
|
||||
if source < dest {
|
||||
(&mut p1[source], &mut p2[0])
|
||||
} else {
|
||||
(&mut p2[0], &mut p1[dest])
|
||||
}
|
||||
}
|
||||
|
||||
let matches = regex.captures(line).unwrap();
|
||||
|
||||
let amount: usize = matches[1].parse().unwrap();
|
||||
|
@ -61,14 +80,7 @@ fn main() {
|
|||
let dest = dest - 1;
|
||||
|
||||
{
|
||||
let (p1, p2) = stacks_9000.split_at_mut(usize::max(source, dest));
|
||||
|
||||
let (source, dest) = if source < dest {
|
||||
(&mut p1[source], &mut p2[0])
|
||||
} else {
|
||||
(&mut p2[0], &mut p1[dest])
|
||||
};
|
||||
|
||||
let (source, dest) = get_stacks(&mut stacks_9000, source, dest);
|
||||
let sourceidx = source.len() - amount;
|
||||
|
||||
dest.extend(source[sourceidx..].iter().copied().rev());
|
||||
|
@ -76,14 +88,7 @@ fn main() {
|
|||
}
|
||||
|
||||
{
|
||||
let (p1, p2) = stacks_9001.split_at_mut(usize::max(source, dest));
|
||||
|
||||
let (source, dest) = if source < dest {
|
||||
(&mut p1[source], &mut p2[0])
|
||||
} else {
|
||||
(&mut p2[0], &mut p1[dest])
|
||||
};
|
||||
|
||||
let (source, dest) = get_stacks(&mut stacks_9001, source, dest);
|
||||
let sourceidx = source.len() - amount;
|
||||
|
||||
dest.extend(source[sourceidx..].iter().copied());
|
||||
|
@ -91,19 +96,10 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
for stacks in [stacks_9000, stacks_9001] {
|
||||
println!(
|
||||
"{}",
|
||||
stacks_9000
|
||||
.iter()
|
||||
.map(|s| s.last().unwrap())
|
||||
.collect::<String>()
|
||||
);
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
stacks_9001
|
||||
.iter()
|
||||
.map(|s| s.last().unwrap())
|
||||
.collect::<String>()
|
||||
stacks.iter().map(|s| s.last().unwrap()).collect::<String>()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue