From 490aa7795be64b532e67665a7dc3023af310f119 Mon Sep 17 00:00:00 2001 From: Kalle Struik Date: Mon, 5 Dec 2022 19:48:49 +0100 Subject: [PATCH] Day 5 --- input/05.txt | 512 ++++++++++++++++++++++++++++++++++++++++++++++++ src/bin/05_1.rs | 108 ++++++++++ src/bin/05_2.rs | 106 ++++++++++ 3 files changed, 726 insertions(+) create mode 100644 input/05.txt create mode 100644 src/bin/05_1.rs create mode 100644 src/bin/05_2.rs diff --git a/input/05.txt b/input/05.txt new file mode 100644 index 0000000..f8e58cb --- /dev/null +++ b/input/05.txt @@ -0,0 +1,512 @@ + [F] [Q] [Q] +[B] [Q] [V] [D] [S] +[S] [P] [T] [R] [M] [D] +[J] [V] [W] [M] [F] [J] [J] +[Z] [G] [S] [W] [N] [D] [R] [T] +[V] [M] [B] [G] [S] [C] [T] [V] [S] +[D] [S] [L] [J] [L] [G] [G] [F] [R] +[G] [Z] [C] [H] [C] [R] [H] [P] [D] + 1 2 3 4 5 6 7 8 9 + +move 3 from 5 to 2 +move 3 from 8 to 4 +move 7 from 7 to 3 +move 14 from 3 to 9 +move 8 from 4 to 1 +move 1 from 7 to 5 +move 2 from 6 to 4 +move 4 from 5 to 7 +move 1 from 3 to 6 +move 3 from 4 to 3 +move 1 from 4 to 1 +move 5 from 1 to 9 +move 1 from 4 to 6 +move 4 from 7 to 4 +move 15 from 9 to 2 +move 7 from 1 to 6 +move 3 from 3 to 5 +move 1 from 4 to 9 +move 2 from 5 to 3 +move 2 from 4 to 9 +move 4 from 1 to 6 +move 1 from 3 to 1 +move 1 from 3 to 2 +move 4 from 6 to 3 +move 24 from 2 to 8 +move 4 from 9 to 8 +move 1 from 1 to 3 +move 2 from 5 to 4 +move 1 from 2 to 4 +move 19 from 8 to 1 +move 5 from 3 to 9 +move 8 from 1 to 3 +move 3 from 4 to 1 +move 6 from 9 to 5 +move 2 from 3 to 4 +move 1 from 8 to 5 +move 2 from 4 to 6 +move 11 from 6 to 1 +move 8 from 8 to 7 +move 1 from 6 to 5 +move 13 from 1 to 3 +move 1 from 1 to 7 +move 2 from 7 to 8 +move 5 from 7 to 1 +move 2 from 8 to 4 +move 3 from 5 to 3 +move 11 from 3 to 1 +move 2 from 5 to 3 +move 2 from 5 to 3 +move 2 from 7 to 1 +move 7 from 3 to 1 +move 1 from 4 to 5 +move 1 from 6 to 4 +move 3 from 4 to 7 +move 3 from 7 to 1 +move 6 from 3 to 5 +move 1 from 5 to 9 +move 4 from 5 to 4 +move 2 from 3 to 4 +move 8 from 9 to 2 +move 5 from 4 to 6 +move 1 from 6 to 5 +move 1 from 4 to 9 +move 39 from 1 to 7 +move 7 from 2 to 6 +move 1 from 9 to 3 +move 1 from 2 to 7 +move 1 from 3 to 1 +move 5 from 7 to 3 +move 4 from 5 to 1 +move 19 from 7 to 9 +move 1 from 9 to 8 +move 1 from 9 to 7 +move 5 from 9 to 3 +move 6 from 6 to 7 +move 1 from 8 to 3 +move 4 from 1 to 4 +move 23 from 7 to 6 +move 1 from 1 to 6 +move 21 from 6 to 2 +move 3 from 4 to 8 +move 7 from 6 to 1 +move 1 from 4 to 9 +move 1 from 6 to 7 +move 6 from 1 to 2 +move 1 from 7 to 4 +move 15 from 2 to 8 +move 5 from 3 to 8 +move 22 from 8 to 7 +move 1 from 8 to 1 +move 5 from 3 to 4 +move 1 from 3 to 2 +move 1 from 1 to 2 +move 3 from 4 to 8 +move 3 from 8 to 9 +move 11 from 2 to 1 +move 2 from 1 to 4 +move 15 from 9 to 5 +move 22 from 7 to 3 +move 2 from 4 to 9 +move 3 from 4 to 2 +move 8 from 1 to 8 +move 6 from 8 to 6 +move 1 from 6 to 2 +move 3 from 6 to 9 +move 3 from 2 to 7 +move 4 from 2 to 9 +move 2 from 7 to 5 +move 1 from 1 to 7 +move 2 from 8 to 2 +move 2 from 7 to 5 +move 9 from 5 to 3 +move 8 from 5 to 2 +move 1 from 6 to 4 +move 1 from 6 to 9 +move 1 from 2 to 9 +move 2 from 5 to 1 +move 7 from 2 to 3 +move 1 from 4 to 3 +move 1 from 2 to 4 +move 5 from 3 to 4 +move 6 from 9 to 3 +move 1 from 2 to 6 +move 6 from 9 to 6 +move 2 from 1 to 8 +move 3 from 6 to 3 +move 2 from 8 to 6 +move 6 from 4 to 1 +move 14 from 3 to 9 +move 1 from 6 to 4 +move 3 from 3 to 9 +move 1 from 4 to 5 +move 10 from 9 to 6 +move 6 from 6 to 7 +move 2 from 1 to 8 +move 1 from 8 to 6 +move 16 from 3 to 2 +move 1 from 8 to 1 +move 1 from 7 to 1 +move 7 from 3 to 4 +move 1 from 6 to 5 +move 4 from 2 to 3 +move 5 from 4 to 9 +move 2 from 4 to 5 +move 4 from 7 to 4 +move 5 from 9 to 6 +move 2 from 5 to 4 +move 11 from 6 to 7 +move 1 from 6 to 8 +move 5 from 1 to 5 +move 2 from 6 to 4 +move 7 from 7 to 3 +move 1 from 8 to 6 +move 2 from 7 to 3 +move 1 from 1 to 3 +move 3 from 2 to 8 +move 9 from 2 to 5 +move 1 from 6 to 1 +move 1 from 4 to 8 +move 7 from 4 to 7 +move 8 from 5 to 6 +move 1 from 7 to 2 +move 1 from 7 to 4 +move 3 from 7 to 8 +move 1 from 2 to 3 +move 1 from 1 to 2 +move 1 from 1 to 7 +move 3 from 7 to 6 +move 11 from 6 to 2 +move 4 from 8 to 7 +move 2 from 8 to 7 +move 15 from 3 to 2 +move 7 from 9 to 4 +move 3 from 3 to 2 +move 4 from 4 to 7 +move 5 from 7 to 3 +move 3 from 4 to 6 +move 3 from 6 to 9 +move 1 from 4 to 2 +move 1 from 8 to 1 +move 2 from 3 to 7 +move 2 from 3 to 7 +move 23 from 2 to 5 +move 1 from 9 to 1 +move 1 from 7 to 9 +move 1 from 1 to 8 +move 8 from 7 to 1 +move 1 from 8 to 4 +move 1 from 4 to 2 +move 3 from 9 to 8 +move 1 from 7 to 9 +move 22 from 5 to 9 +move 1 from 8 to 5 +move 1 from 7 to 4 +move 1 from 4 to 5 +move 1 from 8 to 3 +move 2 from 9 to 3 +move 5 from 5 to 2 +move 5 from 5 to 4 +move 3 from 2 to 7 +move 1 from 7 to 3 +move 6 from 1 to 7 +move 4 from 3 to 1 +move 6 from 2 to 8 +move 1 from 5 to 6 +move 2 from 8 to 1 +move 12 from 9 to 4 +move 8 from 9 to 4 +move 1 from 2 to 9 +move 2 from 9 to 8 +move 3 from 2 to 8 +move 5 from 8 to 6 +move 7 from 7 to 1 +move 4 from 8 to 9 +move 1 from 6 to 1 +move 17 from 4 to 7 +move 1 from 2 to 4 +move 2 from 4 to 1 +move 6 from 4 to 6 +move 1 from 1 to 4 +move 7 from 1 to 5 +move 9 from 7 to 9 +move 8 from 9 to 8 +move 5 from 8 to 3 +move 1 from 5 to 6 +move 2 from 3 to 6 +move 1 from 9 to 1 +move 1 from 6 to 1 +move 10 from 6 to 1 +move 1 from 5 to 1 +move 2 from 9 to 1 +move 1 from 9 to 7 +move 2 from 6 to 8 +move 2 from 8 to 2 +move 1 from 6 to 8 +move 22 from 1 to 9 +move 9 from 7 to 5 +move 1 from 8 to 1 +move 2 from 8 to 3 +move 4 from 5 to 9 +move 1 from 8 to 3 +move 5 from 1 to 9 +move 2 from 7 to 3 +move 2 from 4 to 7 +move 1 from 8 to 5 +move 2 from 2 to 4 +move 1 from 5 to 8 +move 9 from 5 to 8 +move 2 from 7 to 5 +move 2 from 4 to 5 +move 3 from 8 to 4 +move 3 from 4 to 3 +move 2 from 8 to 6 +move 1 from 6 to 4 +move 3 from 5 to 9 +move 1 from 6 to 3 +move 12 from 3 to 5 +move 1 from 3 to 1 +move 7 from 5 to 4 +move 1 from 1 to 3 +move 1 from 8 to 1 +move 7 from 5 to 1 +move 6 from 9 to 6 +move 29 from 9 to 5 +move 2 from 4 to 6 +move 26 from 5 to 2 +move 24 from 2 to 7 +move 1 from 3 to 2 +move 8 from 1 to 7 +move 7 from 6 to 9 +move 2 from 5 to 3 +move 1 from 6 to 4 +move 3 from 8 to 5 +move 2 from 3 to 8 +move 2 from 2 to 8 +move 5 from 9 to 2 +move 27 from 7 to 2 +move 2 from 8 to 3 +move 2 from 9 to 5 +move 3 from 8 to 5 +move 2 from 7 to 4 +move 3 from 4 to 7 +move 2 from 3 to 2 +move 4 from 5 to 1 +move 5 from 7 to 2 +move 29 from 2 to 8 +move 9 from 8 to 3 +move 2 from 4 to 8 +move 7 from 3 to 2 +move 3 from 5 to 4 +move 1 from 7 to 5 +move 3 from 5 to 6 +move 2 from 1 to 8 +move 2 from 6 to 8 +move 3 from 4 to 2 +move 4 from 4 to 2 +move 1 from 6 to 8 +move 8 from 2 to 4 +move 2 from 3 to 5 +move 1 from 4 to 1 +move 3 from 1 to 2 +move 4 from 8 to 2 +move 3 from 4 to 9 +move 3 from 4 to 1 +move 2 from 9 to 5 +move 1 from 4 to 6 +move 4 from 5 to 1 +move 1 from 6 to 8 +move 1 from 9 to 3 +move 4 from 2 to 3 +move 15 from 8 to 2 +move 9 from 8 to 1 +move 1 from 3 to 9 +move 5 from 1 to 9 +move 3 from 9 to 7 +move 2 from 7 to 6 +move 3 from 3 to 2 +move 1 from 7 to 8 +move 1 from 9 to 6 +move 1 from 9 to 8 +move 2 from 8 to 2 +move 1 from 1 to 2 +move 1 from 3 to 7 +move 4 from 1 to 7 +move 19 from 2 to 5 +move 1 from 1 to 4 +move 1 from 7 to 4 +move 1 from 1 to 5 +move 3 from 1 to 4 +move 1 from 1 to 8 +move 6 from 2 to 4 +move 7 from 2 to 1 +move 2 from 7 to 9 +move 8 from 2 to 8 +move 2 from 7 to 3 +move 1 from 6 to 4 +move 10 from 4 to 6 +move 5 from 6 to 7 +move 2 from 9 to 8 +move 6 from 8 to 9 +move 1 from 2 to 3 +move 2 from 8 to 3 +move 5 from 1 to 8 +move 8 from 5 to 2 +move 8 from 8 to 7 +move 7 from 2 to 8 +move 1 from 1 to 2 +move 1 from 9 to 7 +move 1 from 4 to 2 +move 2 from 2 to 6 +move 5 from 9 to 3 +move 2 from 8 to 6 +move 2 from 3 to 9 +move 4 from 8 to 6 +move 7 from 6 to 1 +move 8 from 1 to 5 +move 1 from 8 to 7 +move 1 from 9 to 6 +move 12 from 5 to 3 +move 1 from 4 to 8 +move 2 from 9 to 5 +move 1 from 2 to 3 +move 3 from 5 to 1 +move 1 from 1 to 5 +move 21 from 3 to 8 +move 2 from 1 to 5 +move 6 from 5 to 7 +move 2 from 5 to 6 +move 10 from 6 to 9 +move 1 from 6 to 8 +move 13 from 8 to 2 +move 2 from 5 to 4 +move 2 from 4 to 3 +move 4 from 9 to 1 +move 5 from 7 to 8 +move 12 from 8 to 1 +move 5 from 9 to 6 +move 1 from 3 to 7 +move 2 from 6 to 5 +move 11 from 2 to 1 +move 1 from 8 to 4 +move 16 from 1 to 9 +move 1 from 2 to 6 +move 1 from 8 to 5 +move 12 from 9 to 3 +move 14 from 7 to 2 +move 1 from 7 to 9 +move 1 from 4 to 2 +move 1 from 7 to 5 +move 3 from 9 to 5 +move 4 from 6 to 9 +move 3 from 9 to 4 +move 1 from 8 to 4 +move 2 from 4 to 5 +move 1 from 7 to 1 +move 5 from 3 to 5 +move 2 from 4 to 2 +move 8 from 2 to 7 +move 7 from 2 to 4 +move 1 from 3 to 7 +move 3 from 9 to 7 +move 2 from 2 to 9 +move 3 from 4 to 5 +move 6 from 1 to 8 +move 6 from 1 to 5 +move 3 from 9 to 2 +move 22 from 5 to 9 +move 1 from 5 to 6 +move 2 from 2 to 3 +move 5 from 7 to 6 +move 5 from 8 to 9 +move 2 from 7 to 2 +move 20 from 9 to 4 +move 1 from 8 to 3 +move 2 from 2 to 5 +move 1 from 2 to 5 +move 15 from 4 to 8 +move 1 from 5 to 7 +move 6 from 9 to 1 +move 5 from 4 to 8 +move 2 from 4 to 8 +move 1 from 2 to 1 +move 5 from 6 to 5 +move 5 from 5 to 7 +move 1 from 9 to 8 +move 5 from 7 to 2 +move 2 from 5 to 1 +move 4 from 7 to 5 +move 1 from 5 to 9 +move 1 from 6 to 8 +move 1 from 7 to 2 +move 6 from 3 to 4 +move 3 from 5 to 7 +move 1 from 9 to 2 +move 6 from 2 to 3 +move 1 from 3 to 4 +move 13 from 8 to 9 +move 7 from 1 to 5 +move 6 from 9 to 2 +move 1 from 1 to 4 +move 6 from 2 to 3 +move 1 from 1 to 4 +move 5 from 9 to 7 +move 11 from 8 to 4 +move 7 from 7 to 3 +move 2 from 7 to 8 +move 1 from 8 to 2 +move 8 from 4 to 1 +move 2 from 1 to 6 +move 2 from 5 to 8 +move 3 from 1 to 9 +move 1 from 8 to 2 +move 11 from 3 to 2 +move 2 from 8 to 9 +move 9 from 4 to 7 +move 11 from 3 to 8 +move 7 from 9 to 6 +move 5 from 4 to 6 +move 3 from 7 to 3 +move 1 from 7 to 1 +move 5 from 7 to 6 +move 2 from 3 to 5 +move 1 from 3 to 4 +move 5 from 2 to 5 +move 1 from 1 to 7 +move 1 from 4 to 8 +move 1 from 7 to 6 +move 7 from 5 to 7 +move 2 from 5 to 7 +move 3 from 1 to 7 +move 1 from 2 to 3 +move 1 from 6 to 4 +move 1 from 3 to 4 +move 1 from 5 to 3 +move 18 from 6 to 4 +move 9 from 7 to 1 +move 14 from 4 to 6 +move 3 from 6 to 4 +move 12 from 6 to 7 +move 2 from 5 to 3 +move 3 from 7 to 4 +move 6 from 4 to 7 +move 5 from 1 to 7 +move 5 from 4 to 5 +move 5 from 2 to 1 +move 9 from 8 to 4 +move 9 from 1 to 3 +move 2 from 8 to 2 +move 4 from 2 to 4 +move 1 from 7 to 6 +move 1 from 2 to 3 +move 1 from 8 to 9 +move 1 from 6 to 9 +move 2 from 9 to 3 +move 3 from 4 to 1 +move 13 from 3 to 5 +move 12 from 5 to 1 +move 7 from 1 to 8 +move 1 from 3 to 6 +move 4 from 5 to 4 +move 1 from 5 to 2 +move 8 from 4 to 9 diff --git a/src/bin/05_1.rs b/src/bin/05_1.rs new file mode 100644 index 0000000..9401e59 --- /dev/null +++ b/src/bin/05_1.rs @@ -0,0 +1,108 @@ +use std::fs; + +use anyhow::Result; +use itertools::Itertools; + + +fn main() -> Result<()> { + let input = fs::read_to_string("input/05.txt")?; + + let mut lines = input + .split("\n") + .into_iter(); + + // [F] [Q] [Q] + // [B] [Q] [V] [D] [S] + // [S] [P] [T] [R] [M] [D] + // [J] [V] [W] [M] [F] [J] [J] + // [Z] [G] [S] [W] [N] [D] [R] [T] + // [V] [M] [B] [G] [S] [C] [T] [V] [S] + // [D] [S] [L] [J] [L] [G] [G] [F] [R] + // [G] [Z] [C] [H] [C] [R] [H] [P] [D] + // 1 2 3 4 5 6 7 8 9 + // + // move 3 from 5 to 2 + + let to_strip: &[_] = &[' ', '[', ']']; + + let initial_data = lines + .by_ref() + .take_while(|s| *s != "") + .map(|s| s.chars().collect_vec()) + .map(|c| c.chunks(4).map(|x| x.to_owned()).collect_vec()) + .map(|l| l + .into_iter() + .map(|x| x.into_iter().collect::()) + .collect_vec()) + .map(|l| l.into_iter().map(|x| x.trim_matches(to_strip).to_owned()).collect_vec()) + .take_while(|v| v[0] != "1") + .collect_vec(); + + let initial_data = transpose(&initial_data); + + let stacks = initial_data.into_iter() + .map(|mut l| {l.reverse(); l}) + .map(|l| l.into_iter().take_while(|s| *s != "").collect_vec()) + .collect_vec(); + + let moves = lines + .dropping(1) + .filter(|s| *s != "") + .map(|s| s.split(" ").collect_vec()) + .map(|p| (p[1].parse::().unwrap(), p[3].parse::().unwrap(), p[5].parse::().unwrap())) + .collect_vec(); + + let final_state = moves + .into_iter() + .fold(stacks, execute_move); + + print_state(&final_state); + + let answer = final_state + .into_iter() + .map(|mut l| l.pop().unwrap()) + .join(""); + + println!("{answer}"); + + return Ok(()); +} + + +fn execute_move(mut stacks: Vec>, (count, from, to): (i32, usize, usize)) -> Vec> { + // print_state(&stacks); + // println!("move {count} from {from} to {to}"); + (0..count).for_each(|_| { + let value = stacks[from - 1].pop().unwrap(); + stacks[to - 1].push(value); + }); + + + return stacks; +} + + +fn print_state(stacks: &Vec>) { + let lines = (0..(stacks.iter().map(|v| v.len()).max().unwrap())) + .map(|i| stacks + .iter() + .map(|inner| {if inner.len() > i {inner[i].clone()} else {" ".to_owned()}}) + .collect_vec()) + .collect_vec(); + + lines + .into_iter() + .map(|l| l.into_iter().map(|s| {if s == "" {" ".to_owned()} else {s}}).collect_vec()) + .for_each(|l| println!("{:?}", l)); +} + + +fn transpose(v: &Vec>) -> Vec> +where + T: Clone, +{ + assert!(!v.is_empty()); + (0..v[0].len()) + .map(|i| v.iter().map(|inner| inner[i].clone()).collect::>()) + .collect() +} diff --git a/src/bin/05_2.rs b/src/bin/05_2.rs new file mode 100644 index 0000000..7a957d7 --- /dev/null +++ b/src/bin/05_2.rs @@ -0,0 +1,106 @@ +use std::fs; + +use anyhow::Result; +use itertools::Itertools; + + +fn main() -> Result<()> { + let input = fs::read_to_string("input/05.txt")?; + + let mut lines = input + .split("\n") + .into_iter(); + + // [F] [Q] [Q] + // [B] [Q] [V] [D] [S] + // [S] [P] [T] [R] [M] [D] + // [J] [V] [W] [M] [F] [J] [J] + // [Z] [G] [S] [W] [N] [D] [R] [T] + // [V] [M] [B] [G] [S] [C] [T] [V] [S] + // [D] [S] [L] [J] [L] [G] [G] [F] [R] + // [G] [Z] [C] [H] [C] [R] [H] [P] [D] + // 1 2 3 4 5 6 7 8 9 + // + // move 3 from 5 to 2 + + let to_strip: &[_] = &[' ', '[', ']']; + + let initial_data = lines + .by_ref() + .take_while(|s| *s != "") + .map(|s| s.chars().collect_vec()) + .map(|c| c.chunks(4).map(|x| x.to_owned()).collect_vec()) + .map(|l| l + .into_iter() + .map(|x| x.into_iter().collect::()) + .collect_vec()) + .map(|l| l.into_iter().map(|x| x.trim_matches(to_strip).to_owned()).collect_vec()) + .take_while(|v| v[0] != "1") + .collect_vec(); + + let initial_data = transpose(&initial_data); + + let stacks = initial_data.into_iter() + .map(|mut l| {l.reverse(); l}) + .map(|l| l.into_iter().take_while(|s| *s != "").collect_vec()) + .collect_vec(); + + let moves = lines + .dropping(1) + .filter(|s| *s != "") + .map(|s| s.split(" ").collect_vec()) + .map(|p| (p[1].parse::().unwrap(), p[3].parse::().unwrap(), p[5].parse::().unwrap())) + .collect_vec(); + + let final_state = moves + .into_iter() + .fold(stacks, execute_move); + + print_state(&final_state); + + let answer = final_state + .into_iter() + .map(|mut l| l.pop().unwrap()) + .join(""); + + println!("{answer}"); + + return Ok(()); +} + + +fn execute_move(mut stacks: Vec>, (count, from, to): (usize, usize, usize)) -> Vec> { + // print_state(&stacks); + // println!("move {count} from {from} to {to}"); + let mut values = (0..count).map(|_| stacks[from - 1].pop().unwrap()).collect_vec(); + values.reverse(); + values.into_iter().for_each(|v| stacks[to - 1].push(v)); + + return stacks; +} + + +fn print_state(stacks: &Vec>) { + let lines = (0..(stacks.iter().map(|v| v.len()).max().unwrap())) + .map(|i| stacks + .iter() + .map(|inner| {if inner.len() > i {inner[i].clone()} else {" ".to_owned()}}) + .collect_vec()) + .collect_vec(); + + lines + .into_iter() + .map(|l| l.into_iter().map(|s| {if s == "" {" ".to_owned()} else {s}}).collect_vec()) + .for_each(|l| println!("{:?}", l)); +} + + +fn transpose(v: &Vec>) -> Vec> +where + T: Clone, +{ + assert!(!v.is_empty()); + (0..v[0].len()) + .map(|i| v.iter().map(|inner| inner[i].clone()).collect::>()) + .collect() +}