diff options
author | j-james | 2022-12-05 10:23:07 +0000 |
---|---|---|
committer | j-james | 2022-12-05 10:23:07 +0000 |
commit | 8ebbb44dd19639cba8e25aaba5ffe00ba957c58f (patch) | |
tree | 0ffcd32e207f89831abaabd52485799c718e53d4 /2022/rust | |
parent | 82be8473d21c15bc22c46cc4ba5463e3d2213c7d (diff) |
Day Five
Diffstat (limited to '2022/rust')
-rw-r--r-- | 2022/rust/day05/Cargo.lock | 16 | ||||
-rw-r--r-- | 2022/rust/day05/Cargo.toml | 9 | ||||
-rw-r--r-- | 2022/rust/day05/src/main.rs | 54 |
3 files changed, 79 insertions, 0 deletions
diff --git a/2022/rust/day05/Cargo.lock b/2022/rust/day05/Cargo.lock new file mode 100644 index 0000000..92653ff --- /dev/null +++ b/2022/rust/day05/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day05" +version = "0.1.0" +dependencies = [ + "text_io", +] + +[[package]] +name = "text_io" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5f0c8eb2ad70c12a6a69508f499b3051c924f4b1cfeae85bfad96e6bc5bba46" diff --git a/2022/rust/day05/Cargo.toml b/2022/rust/day05/Cargo.toml new file mode 100644 index 0000000..4cc1d88 --- /dev/null +++ b/2022/rust/day05/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day05" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +text_io = "0.1.12" diff --git a/2022/rust/day05/src/main.rs b/2022/rust/day05/src/main.rs new file mode 100644 index 0000000..b13d16e --- /dev/null +++ b/2022/rust/day05/src/main.rs @@ -0,0 +1,54 @@ +use std::collections::VecDeque; +use std::env; +use std::fs; +use text_io::scan; + +fn main() { + let args = env::args().nth(1).expect("missing input"); + let input = fs::read_to_string(args).unwrap(); + let mut parts = input.split("\n\n"); + + let crates = parts.next().unwrap(); + // yes, it's gross. yes, it works. no, i couldn't think of anything better. + let mut stacks: Vec<Vec<char>> = vec![]; + for i in 0..9 { + let mut stack: Vec<char> = vec![]; + for j in 0..8 { + stack.push(crates.chars().nth((9*4) * (7-j) + 4*i + 1).unwrap()); + } + while *stack.last().unwrap() == ' ' { + stack.pop(); + } + stacks.push(stack); + } + + let instructions: Vec<[usize; 3]> = parts.next().unwrap().trim().split("\n").map(|x| { + let a: usize; + let b: usize; + let c: usize; + scan!(x.bytes() => "move {} from {} to {}", a, b, c); + return [a, b, c]; + }).collect(); // collect() still messes me up + let capacity: usize = instructions.iter().fold(0, |x,y| x.max(y[0])); + + let mut stack = stacks.clone(); + for ins in &instructions { + for _ in 0 .. ins[0] { + let cargo = stack[ins[1]-1].pop().unwrap(); + stack[ins[2]-1].push(cargo); + } + } + let answer: String = stack.iter().map(|x| x.last().unwrap()).collect(); + println!("{}", answer); + + let mut stack = stacks.clone(); + for ins in instructions { + let mut cargo: VecDeque<char> = VecDeque::with_capacity(capacity); + for _ in 0 .. ins[0] { + cargo.push_front(stack[ins[1]-1].pop().unwrap()); + } + stack[ins[2]-1].append(&mut cargo.into()); + } + let answer: String = stack.iter().map(|x| x.last().unwrap()).collect(); + println!("{}", answer); +} |