Codinget 2 years ago
parent 793b6f4033
commit e225f631ce
  1. 7
      d5/Cargo.lock
  2. 8
      d5/Cargo.toml
  3. 65
      d5/src/main.rs

7
d5/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "d5"
version = "0.1.0"

@ -0,0 +1,8 @@
[package]
name = "d5"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,65 @@
use std::{fs::read_to_string, io::Result};
fn main() -> Result<()> {
let mut stacks = Vec::new();
let mut stacks2 = Vec::new();
for line in read_to_string("input.txt")?.split("\n") {
if line.is_empty() {
continue;
}
let bytes = line.as_bytes();
if bytes[0] == 0x5b {
// row of crates
let len = (bytes.len() + 1) / 4;
while stacks.len() < len {
stacks.push(Vec::new())
}
for i in 0..len {
let c = bytes[4 * i + 1] as char;
if c == ' ' {
continue;
}
stacks[i].insert(0, c);
}
} else if bytes[0] == 0x20 {
// crate ids
stacks2 = stacks.clone();
} else {
// move op
let parts = line.split(" ").collect::<Vec<_>>();
let count = parts[1].parse().unwrap();
let src: usize = parts[3].parse().unwrap();
let dst: usize = parts[5].parse().unwrap();
for _ in 0..count {
let item = stacks[src - 1].pop().unwrap();
stacks[dst - 1].push(item);
}
let src_len = stacks2[src - 1].len();
let mut collected = stacks2[src - 1]
.drain(src_len - count..)
.collect::<Vec<_>>();
collected.drain(..).for_each(|x| stacks2[dst - 1].push(x));
}
}
println!(
"Part 1: {}",
stacks
.iter()
.map(|stack| stack[stack.len() - 1])
.collect::<String>()
);
println!(
"Part 2: {}",
stacks2
.iter()
.map(|stack| stack[stack.len() - 1])
.collect::<String>()
);
Ok(())
}
Loading…
Cancel
Save