diff --git a/src/bin/05.rs b/src/bin/05.rs index cf50e83..0a86bdc 100644 --- a/src/bin/05.rs +++ b/src/bin/05.rs @@ -56,9 +56,21 @@ pub fn part_one(input: &str) -> Option { } pub fn part_two(input: &str) -> Option { - let _ = input; + let parts: Vec<&str> = input.split("\n\n").collect(); + let mut stacks = parse_stacks(parts[0]); + let instructions = parse_instructions(parts[1]); + for i in instructions { + let mut new = Vec::new(); - None + for _ in 0..(i.amount) { + let x = stacks[i.from-1].pop().unwrap(); + new.insert(0,x); + } + stacks[i.to-1].append(&mut new); + } + Some(stacks.iter() + .map(| x | x[x.len()-1].to_string()) // stringify top element of every stack + .reduce(| x, y | x + &y).unwrap()) // and merge into one big string } fn main() { @@ -80,6 +92,6 @@ mod tests { #[test] fn test_part_two() { let input = advent_of_code::read_file("examples", 5); - assert_eq!(part_two(&input), None); + assert_eq!(part_two(&input), Some("MCD".to_string())); } }