commit 0188b5b860d24386758d6a703186053a85686cf2 parent 264f4449066c78ef7adb55e72c3259852e73a82d Author: Shimmy Xu <shimmy.xu@shimmy1996.com> Date: Mon, 5 Dec 2022 02:00:18 -0500 Add 2022 day 05 Diffstat:
A | 2022/day05/Cargo.toml | | | 8 | ++++++++ |
A | 2022/day05/input.txt | | | 511 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2022/day05/src/main.rs | | | 56 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 575 insertions(+), 0 deletions(-) diff --git a/2022/day05/Cargo.toml b/2022/day05/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day05" +version = "0.1.0" +edition = "2021" + +[dependencies] +regex = "1"+ \ No newline at end of file diff --git a/2022/day05/input.txt b/2022/day05/input.txt @@ -0,0 +1,511 @@ +[V] [B] [C] +[C] [N] [G] [W] [P] +[W] [C] [Q] [S] [C] [M] +[L] [W] [B] [Z] [F] [S] [V] +[R] [G] [H] [F] [P] [V] [M] [T] +[M] [L] [R] [D] [L] [N] [P] [D] [W] +[F] [Q] [S] [C] [G] [G] [Z] [P] [N] +[Q] [D] [P] [L] [V] [D] [D] [C] [Z] + 1 2 3 4 5 6 7 8 9 + +move 1 from 9 to 2 +move 4 from 6 to 1 +move 4 from 2 to 6 +move 5 from 8 to 7 +move 4 from 9 to 2 +move 1 from 5 to 8 +move 1 from 3 to 1 +move 2 from 3 to 1 +move 1 from 4 to 2 +move 11 from 7 to 2 +move 5 from 5 to 1 +move 1 from 6 to 8 +move 1 from 7 to 6 +move 3 from 6 to 7 +move 1 from 3 to 2 +move 1 from 6 to 8 +move 11 from 2 to 1 +move 1 from 9 to 8 +move 1 from 3 to 7 +move 4 from 7 to 9 +move 3 from 3 to 7 +move 4 from 8 to 2 +move 3 from 7 to 6 +move 2 from 6 to 3 +move 5 from 4 to 1 +move 1 from 6 to 5 +move 26 from 1 to 7 +move 1 from 4 to 6 +move 22 from 7 to 5 +move 4 from 9 to 1 +move 3 from 7 to 3 +move 1 from 6 to 3 +move 6 from 1 to 7 +move 2 from 7 to 5 +move 8 from 1 to 9 +move 4 from 3 to 4 +move 10 from 2 to 7 +move 6 from 7 to 4 +move 2 from 9 to 5 +move 1 from 5 to 1 +move 8 from 4 to 1 +move 2 from 5 to 9 +move 1 from 3 to 6 +move 1 from 9 to 1 +move 1 from 3 to 6 +move 2 from 5 to 2 +move 1 from 4 to 2 +move 1 from 2 to 3 +move 7 from 1 to 4 +move 9 from 7 to 4 +move 1 from 3 to 4 +move 2 from 2 to 4 +move 5 from 9 to 6 +move 1 from 4 to 5 +move 2 from 9 to 3 +move 1 from 1 to 6 +move 2 from 6 to 1 +move 2 from 6 to 5 +move 2 from 9 to 7 +move 1 from 3 to 9 +move 1 from 9 to 5 +move 2 from 7 to 3 +move 1 from 1 to 7 +move 7 from 4 to 5 +move 2 from 1 to 2 +move 3 from 3 to 8 +move 3 from 8 to 9 +move 31 from 5 to 8 +move 1 from 7 to 1 +move 1 from 2 to 1 +move 1 from 1 to 5 +move 1 from 5 to 6 +move 2 from 5 to 7 +move 10 from 4 to 9 +move 5 from 6 to 2 +move 3 from 2 to 6 +move 2 from 7 to 8 +move 1 from 6 to 3 +move 1 from 4 to 1 +move 1 from 3 to 6 +move 1 from 4 to 2 +move 2 from 1 to 2 +move 1 from 8 to 7 +move 10 from 8 to 2 +move 13 from 2 to 9 +move 1 from 1 to 5 +move 18 from 8 to 2 +move 21 from 9 to 6 +move 1 from 7 to 8 +move 2 from 9 to 7 +move 1 from 2 to 3 +move 1 from 7 to 8 +move 9 from 2 to 4 +move 1 from 7 to 8 +move 3 from 9 to 1 +move 1 from 8 to 1 +move 6 from 2 to 3 +move 5 from 4 to 7 +move 1 from 5 to 8 +move 2 from 4 to 3 +move 5 from 7 to 3 +move 2 from 2 to 7 +move 15 from 6 to 1 +move 12 from 1 to 2 +move 6 from 2 to 9 +move 4 from 9 to 5 +move 4 from 5 to 6 +move 14 from 3 to 9 +move 1 from 6 to 7 +move 1 from 7 to 2 +move 1 from 7 to 8 +move 9 from 2 to 6 +move 1 from 1 to 6 +move 2 from 9 to 8 +move 4 from 9 to 7 +move 1 from 1 to 5 +move 8 from 8 to 3 +move 1 from 5 to 4 +move 2 from 1 to 2 +move 3 from 1 to 4 +move 9 from 6 to 2 +move 1 from 7 to 4 +move 1 from 8 to 2 +move 1 from 6 to 4 +move 4 from 7 to 8 +move 12 from 6 to 8 +move 3 from 2 to 1 +move 6 from 8 to 7 +move 5 from 3 to 6 +move 3 from 3 to 6 +move 3 from 1 to 3 +move 8 from 2 to 9 +move 2 from 4 to 5 +move 2 from 7 to 2 +move 10 from 8 to 5 +move 3 from 3 to 2 +move 10 from 5 to 3 +move 1 from 4 to 3 +move 1 from 2 to 1 +move 1 from 1 to 7 +move 14 from 9 to 6 +move 5 from 2 to 4 +move 15 from 6 to 5 +move 3 from 9 to 3 +move 1 from 8 to 6 +move 1 from 3 to 8 +move 7 from 3 to 8 +move 16 from 5 to 1 +move 2 from 7 to 1 +move 1 from 5 to 9 +move 2 from 9 to 3 +move 15 from 1 to 5 +move 3 from 8 to 2 +move 3 from 3 to 1 +move 3 from 7 to 3 +move 8 from 4 to 6 +move 5 from 1 to 6 +move 9 from 5 to 7 +move 2 from 8 to 3 +move 2 from 2 to 7 +move 1 from 1 to 4 +move 2 from 5 to 8 +move 4 from 3 to 1 +move 4 from 8 to 1 +move 1 from 8 to 6 +move 9 from 7 to 6 +move 2 from 7 to 5 +move 3 from 1 to 8 +move 1 from 4 to 8 +move 1 from 2 to 4 +move 12 from 6 to 2 +move 3 from 8 to 6 +move 1 from 4 to 7 +move 2 from 6 to 8 +move 5 from 5 to 9 +move 13 from 2 to 9 +move 2 from 4 to 7 +move 13 from 9 to 5 +move 2 from 6 to 5 +move 1 from 3 to 9 +move 6 from 9 to 4 +move 5 from 1 to 3 +move 1 from 7 to 9 +move 15 from 5 to 8 +move 2 from 4 to 7 +move 2 from 4 to 6 +move 1 from 4 to 6 +move 1 from 5 to 7 +move 18 from 6 to 2 +move 2 from 7 to 3 +move 3 from 6 to 7 +move 3 from 2 to 8 +move 5 from 7 to 3 +move 1 from 9 to 6 +move 2 from 3 to 8 +move 11 from 3 to 2 +move 2 from 2 to 9 +move 1 from 6 to 2 +move 1 from 7 to 5 +move 1 from 5 to 9 +move 9 from 8 to 4 +move 1 from 4 to 6 +move 2 from 3 to 1 +move 2 from 1 to 5 +move 12 from 8 to 3 +move 1 from 8 to 2 +move 14 from 3 to 4 +move 1 from 6 to 4 +move 1 from 5 to 4 +move 20 from 2 to 7 +move 2 from 9 to 5 +move 1 from 5 to 3 +move 1 from 9 to 2 +move 1 from 2 to 8 +move 2 from 2 to 3 +move 5 from 4 to 5 +move 6 from 5 to 7 +move 2 from 8 to 2 +move 3 from 3 to 9 +move 5 from 4 to 5 +move 2 from 9 to 7 +move 2 from 2 to 3 +move 1 from 9 to 3 +move 22 from 7 to 3 +move 4 from 7 to 4 +move 24 from 3 to 6 +move 4 from 2 to 6 +move 18 from 6 to 9 +move 15 from 4 to 6 +move 8 from 6 to 3 +move 6 from 6 to 1 +move 7 from 9 to 6 +move 2 from 7 to 4 +move 8 from 3 to 9 +move 14 from 6 to 3 +move 2 from 3 to 9 +move 1 from 9 to 6 +move 13 from 9 to 1 +move 3 from 4 to 5 +move 1 from 9 to 6 +move 5 from 1 to 8 +move 3 from 3 to 9 +move 2 from 1 to 5 +move 8 from 5 to 8 +move 10 from 3 to 5 +move 3 from 4 to 6 +move 6 from 1 to 9 +move 4 from 5 to 3 +move 5 from 8 to 2 +move 6 from 6 to 3 +move 7 from 3 to 6 +move 1 from 3 to 4 +move 5 from 8 to 7 +move 5 from 2 to 6 +move 2 from 7 to 3 +move 3 from 7 to 3 +move 1 from 4 to 9 +move 9 from 6 to 9 +move 2 from 6 to 2 +move 1 from 8 to 2 +move 2 from 8 to 7 +move 5 from 1 to 5 +move 1 from 1 to 4 +move 13 from 5 to 7 +move 5 from 3 to 7 +move 1 from 5 to 6 +move 1 from 4 to 6 +move 3 from 2 to 8 +move 1 from 3 to 5 +move 1 from 3 to 8 +move 14 from 7 to 4 +move 1 from 5 to 6 +move 7 from 6 to 9 +move 6 from 7 to 9 +move 2 from 8 to 9 +move 2 from 8 to 1 +move 31 from 9 to 1 +move 13 from 4 to 2 +move 1 from 4 to 3 +move 10 from 2 to 7 +move 1 from 3 to 4 +move 1 from 2 to 7 +move 3 from 7 to 8 +move 1 from 4 to 1 +move 3 from 8 to 5 +move 32 from 1 to 5 +move 3 from 9 to 7 +move 4 from 9 to 6 +move 2 from 2 to 7 +move 2 from 1 to 7 +move 1 from 6 to 1 +move 1 from 9 to 4 +move 3 from 6 to 4 +move 1 from 1 to 8 +move 15 from 5 to 1 +move 1 from 8 to 4 +move 9 from 5 to 7 +move 1 from 9 to 8 +move 1 from 8 to 1 +move 10 from 1 to 9 +move 1 from 4 to 2 +move 2 from 9 to 5 +move 4 from 9 to 6 +move 1 from 2 to 7 +move 3 from 4 to 2 +move 1 from 1 to 5 +move 5 from 1 to 5 +move 1 from 4 to 9 +move 3 from 6 to 7 +move 23 from 7 to 6 +move 1 from 2 to 4 +move 1 from 2 to 5 +move 9 from 5 to 4 +move 1 from 2 to 5 +move 9 from 5 to 6 +move 1 from 9 to 7 +move 1 from 9 to 3 +move 3 from 9 to 4 +move 14 from 6 to 3 +move 5 from 7 to 4 +move 1 from 7 to 5 +move 1 from 5 to 9 +move 2 from 5 to 6 +move 16 from 6 to 2 +move 2 from 6 to 1 +move 7 from 4 to 8 +move 2 from 1 to 2 +move 4 from 3 to 5 +move 5 from 4 to 7 +move 2 from 6 to 7 +move 4 from 4 to 1 +move 4 from 8 to 9 +move 1 from 4 to 5 +move 1 from 6 to 8 +move 1 from 4 to 9 +move 4 from 1 to 7 +move 1 from 9 to 4 +move 2 from 2 to 7 +move 7 from 3 to 9 +move 15 from 2 to 3 +move 4 from 8 to 6 +move 1 from 4 to 7 +move 2 from 9 to 7 +move 1 from 6 to 8 +move 2 from 7 to 2 +move 5 from 7 to 2 +move 1 from 5 to 2 +move 6 from 2 to 9 +move 3 from 7 to 1 +move 3 from 1 to 2 +move 3 from 7 to 1 +move 2 from 2 to 9 +move 2 from 6 to 9 +move 1 from 8 to 3 +move 19 from 3 to 9 +move 1 from 6 to 3 +move 3 from 7 to 4 +move 1 from 2 to 5 +move 2 from 1 to 9 +move 2 from 2 to 3 +move 33 from 9 to 7 +move 1 from 1 to 7 +move 3 from 3 to 7 +move 1 from 3 to 2 +move 1 from 5 to 8 +move 4 from 9 to 7 +move 1 from 5 to 2 +move 2 from 4 to 9 +move 4 from 9 to 7 +move 3 from 2 to 1 +move 1 from 4 to 3 +move 1 from 9 to 7 +move 1 from 8 to 3 +move 7 from 7 to 3 +move 3 from 1 to 9 +move 4 from 9 to 7 +move 4 from 5 to 8 +move 3 from 3 to 4 +move 3 from 4 to 5 +move 3 from 3 to 6 +move 2 from 6 to 5 +move 38 from 7 to 5 +move 40 from 5 to 3 +move 4 from 8 to 9 +move 1 from 6 to 9 +move 1 from 5 to 1 +move 3 from 7 to 6 +move 1 from 7 to 5 +move 38 from 3 to 8 +move 1 from 1 to 9 +move 3 from 9 to 6 +move 5 from 3 to 9 +move 4 from 8 to 6 +move 1 from 7 to 1 +move 3 from 5 to 9 +move 1 from 1 to 2 +move 10 from 8 to 3 +move 5 from 8 to 1 +move 3 from 1 to 2 +move 9 from 6 to 7 +move 9 from 3 to 5 +move 1 from 7 to 6 +move 1 from 3 to 8 +move 1 from 7 to 9 +move 1 from 1 to 5 +move 1 from 1 to 3 +move 1 from 9 to 2 +move 4 from 2 to 3 +move 1 from 2 to 4 +move 9 from 8 to 1 +move 2 from 9 to 5 +move 2 from 1 to 2 +move 2 from 3 to 4 +move 6 from 8 to 6 +move 10 from 5 to 3 +move 7 from 3 to 2 +move 2 from 1 to 2 +move 5 from 1 to 7 +move 7 from 9 to 6 +move 7 from 6 to 5 +move 1 from 4 to 3 +move 7 from 7 to 4 +move 5 from 3 to 9 +move 7 from 2 to 6 +move 4 from 7 to 8 +move 5 from 8 to 9 +move 1 from 2 to 6 +move 1 from 3 to 5 +move 2 from 2 to 8 +move 8 from 4 to 6 +move 7 from 9 to 7 +move 4 from 7 to 9 +move 7 from 9 to 3 +move 8 from 3 to 1 +move 6 from 5 to 9 +move 8 from 1 to 8 +move 13 from 8 to 4 +move 3 from 9 to 6 +move 1 from 8 to 6 +move 1 from 7 to 3 +move 2 from 4 to 1 +move 5 from 9 to 1 +move 1 from 3 to 7 +move 15 from 6 to 1 +move 1 from 7 to 9 +move 10 from 4 to 7 +move 11 from 7 to 5 +move 17 from 1 to 6 +move 1 from 9 to 3 +move 6 from 6 to 1 +move 3 from 5 to 3 +move 2 from 4 to 5 +move 2 from 7 to 8 +move 12 from 5 to 3 +move 13 from 6 to 9 +move 2 from 8 to 2 +move 2 from 5 to 1 +move 16 from 3 to 8 +move 3 from 2 to 3 +move 2 from 3 to 7 +move 2 from 7 to 9 +move 1 from 3 to 7 +move 4 from 8 to 4 +move 2 from 4 to 8 +move 5 from 1 to 5 +move 2 from 4 to 7 +move 6 from 6 to 8 +move 2 from 8 to 5 +move 2 from 1 to 4 +move 5 from 8 to 7 +move 5 from 6 to 3 +move 6 from 9 to 8 +move 2 from 9 to 2 +move 1 from 1 to 7 +move 4 from 5 to 3 +move 2 from 2 to 3 +move 1 from 4 to 9 +move 10 from 3 to 6 +move 1 from 3 to 7 +move 10 from 7 to 2 +move 2 from 5 to 3 +move 1 from 4 to 2 +move 2 from 6 to 8 +move 3 from 6 to 5 +move 1 from 6 to 1 +move 7 from 2 to 3 +move 6 from 8 to 7 +move 4 from 6 to 3 +move 14 from 8 to 6 +move 11 from 6 to 8 +move 1 from 1 to 4 +move 6 from 7 to 2 +move 3 from 5 to 8 +move 4 from 1 to 7 +move 1 from 2 to 8 +move 1 from 2 to 6 +move 1 from 3 to 4 +move 1 from 5 to 6 +move 7 from 8 to 6 +move 9 from 3 to 2 +move 1 from 8 to 5 diff --git a/2022/day05/src/main.rs b/2022/day05/src/main.rs @@ -0,0 +1,56 @@ +use regex::Regex; +use std::collections::VecDeque; + +fn main() { + let input_raw = std::fs::read_to_string("input.txt").unwrap(); + let stacks_raw = input_raw.trim().split("\n\n").nth(0).unwrap(); + let stack_count = (stacks_raw.split('\n').last().unwrap().len() + 1) / 4; + let re = Regex::new(r"[A-Z]").unwrap(); + let stacks = { + let mut tmp = (0..stack_count) + .map(|_| VecDeque::new()) + .collect::<Vec<VecDeque<char>>>(); + for l in stacks_raw.split('\n').rev().skip(1) { + for m in re.find_iter(l) { + let i = (m.start() - 1) / 4; + tmp[i].push_back(l.chars().nth(m.start()).unwrap()); + } + } + tmp + }; + let re = Regex::new(r"^move ([0-9]+) from ([0-9]+) to ([0-9]+)$").unwrap(); + let procedure = (*input_raw.trim().trim().split("\n\n").nth(1).unwrap()) + .split('\n') + .map(|x| { + re.captures(x) + .unwrap() + .iter() + .skip(1) + .map(|c| c.unwrap().as_str().parse::<usize>().unwrap()) + .collect() + }) + .collect::<Vec<Vec<usize>>>(); + // VWLCWGSDQ + println!("Part 1: {}", part_1(stacks.clone(), &procedure)); + // TCGLQSLPW + println!("Part 2: {}", part_2(stacks.clone(), &procedure)); +} + +fn part_1(mut stacks: Vec<VecDeque<char>>, procedure: &Vec<Vec<usize>>) -> String { + for p in procedure { + for _ in 0..p[0] { + let c = stacks[p[1] - 1].pop_back().unwrap(); + stacks[p[2] - 1].push_back(c); + } + } + stacks.iter().map(|s| s.back().unwrap()).collect() +} + +fn part_2(mut stacks: Vec<VecDeque<char>>, procedure: &Vec<Vec<usize>>) -> String { + for p in procedure { + let ii = stacks[p[1] - 1].len() - p[0]; + let mut cc = stacks[p[1] - 1].split_off(ii); + stacks[p[2] - 1].append(&mut cc); + } + stacks.iter().map(|s| s.back().unwrap()).collect() +}