Skip to content

Instantly share code, notes, and snippets.

@saolsen
Created December 5, 2022 13:41
Show Gist options
  • Save saolsen/04e450e3a202e07146911fa40a78cbf1 to your computer and use it in GitHub Desktop.
Save saolsen/04e450e3a202e07146911fa40a78cbf1 to your computer and use it in GitHub Desktop.
aoc 2022 day05
fn main() {
let input = include_str!("day05_input.txt");
let lines = input.split("\n").collect::<Vec<&str>>();
// find where the file switches from stacks to instructions
let mut split = 0;
for (i, line) in lines.iter().enumerate() {
if line.is_empty() {
split = i;
}
}
// note: hardcoded size.
let mut stacks = vec![vec![]; 9];
// build stacks
for i in (0..=(split - 2)).rev() {
let line = lines[i];
for j in (0..line.len()).step_by(4) {
let slot = &line[j + 1..j + 2];
if slot == " " {
} else {
let stack = &mut stacks[(j / 4)];
stack.push(slot);
}
}
}
for line in &lines[split + 1..lines.len()] {
let words = line.split(" ").collect::<Vec<&str>>();
let num: usize = words[1].parse().unwrap();
let mut from: usize = words[3].parse().unwrap();
from -= 1;
let mut to: usize = words[5].parse().unwrap();
to -= 1;
// part 1
// for _ in 0..num {
// let elem = stacks[from].pop().unwrap();
// stacks[to].push(elem);
// }
// part 2
let mut buf = vec![];
for _ in 0..num {
let elem = stacks[from].pop().unwrap();
buf.push(elem);
}
buf.reverse();
stacks[to].append(&mut buf);
}
// print answer
for i in 0..stacks.len() {
eprint!("{}", stacks[i].pop().unwrap());
}
eprintln!();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment