Second star.
Too lazy to figure out proper cloning of the Dock so we just parse it twice, whatever
This commit is contained in:
parent
6e9261e027
commit
d88886869a
1 changed files with 52 additions and 12 deletions
|
@ -50,27 +50,58 @@ impl FromStr for CrateStack {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Dock(Vec<CrateStack>);
|
struct Dock(Vec<CrateStack>);
|
||||||
|
|
||||||
|
trait Crane {
|
||||||
|
fn run_directive(dock: &mut Dock, directive: &MoveDirective) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
impl Dock {
|
impl Dock {
|
||||||
fn perform_directive(&mut self, directive: &MoveDirective) -> bool {
|
fn tops(&self) -> impl Iterator<Item = Option<&Crate>> {
|
||||||
if directive.source >= self.0.len() {
|
self.0.iter().map(|s| s.0.iter().last())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct CrateMover9000;
|
||||||
|
|
||||||
|
impl Crane for CrateMover9000 {
|
||||||
|
fn run_directive(dock: &mut Dock, directive: &MoveDirective) -> bool {
|
||||||
|
if directive.source >= dock.0.len() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if directive.dest >= self.0.len() {
|
if directive.dest >= dock.0.len() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for _ in 0..directive.number {
|
for _ in 0..directive.number {
|
||||||
if let Some(s) = self.0[directive.source].pop() {
|
if let Some(s) = dock.0[directive.source].pop() {
|
||||||
self.0[directive.dest].push(s)
|
dock.0[directive.dest].push(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn tops(&self) -> impl Iterator<Item = Option<&Crate>> {
|
struct CrateMover9001;
|
||||||
self.0.iter().map(|s| s.0.iter().last())
|
|
||||||
|
impl Crane for CrateMover9001 {
|
||||||
|
fn run_directive(dock: &mut Dock, directive: &MoveDirective) -> bool {
|
||||||
|
if directive.source >= dock.0.len() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if directive.dest >= dock.0.len() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let source_vec = &mut dock.0[directive.source].0;
|
||||||
|
|
||||||
|
let drained: Vec<Crate> = source_vec
|
||||||
|
.drain((source_vec.len() - usize::try_from(directive.number).unwrap())..)
|
||||||
|
.collect();
|
||||||
|
dock.0[directive.dest].0.extend(drained);
|
||||||
|
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,20 +189,29 @@ fn main() {
|
||||||
println!("PART ONE ---------------");
|
println!("PART ONE ---------------");
|
||||||
part1(dock, &moves);
|
part1(dock, &moves);
|
||||||
|
|
||||||
|
let dock2: Dock = string
|
||||||
|
.split("\n\n")
|
||||||
|
.next()
|
||||||
|
.expect("first section")
|
||||||
|
.parse::<Dock>()
|
||||||
|
.expect("can't parse dock");
|
||||||
|
|
||||||
println!("\n PART TWO ---------------");
|
println!("\n PART TWO ---------------");
|
||||||
|
part2(dock2, &moves);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(mut dock: Dock, moves: &[MoveDirective]) {
|
fn part1(mut dock: Dock, moves: &[MoveDirective]) {
|
||||||
for m in moves {
|
for m in moves {
|
||||||
dock.perform_directive(m);
|
CrateMover9000::run_directive(&mut dock, m);
|
||||||
}
|
}
|
||||||
|
|
||||||
dbg!(dock.tops().flatten().map(|c| c.0).collect::<String>());
|
dbg!(dock.tops().flatten().map(|c| c.0).collect::<String>());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2() {}
|
fn part2(mut dock: Dock, moves: &[MoveDirective]) {
|
||||||
|
for m in moves {
|
||||||
|
CrateMover9001::run_directive(&mut dock, m);
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
dbg!(dock.tops().flatten().map(|c| c.0).collect::<String>());
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue