Advent of Code --- day 5

Advent of Code是一个年度编程挑战活动,本文介绍了day 5的问题,涉及货箱堆叠的重新排列。文章通过示例详细解释了如何根据操作指令重新组织货箱堆栈,并探讨了当使用CrateMover 9001(一款能一次性移动多个货箱的设备)时,货箱顺序的变化,以确定最终每个堆栈顶部的货箱是什么。
摘要由CSDN通过智能技术生成

最近经常有人提到 Advent of Code, 不知道是啥, 今天看了一下, 发现原来是一年一度的‘盛事’, 凑个热闹, 刚打开的时候 day-6 的题还要 2 个小时才公布, 于是就把 day-5 做了一下。整体感觉比 LeetCode 里的题要轻松, 当然据说难度也是循序渐进, 后面的题可能就不那么好解了。而且 aoc 可能用到更多的标准库,你要自己去处理 input,最终提交的也不是代码而是答案, 所以整体的自由度更高一些。题目比较简单, 就不做过多解释了, 直接上代码。


Day 5: Supply Stacks

The expedition can depart as soon as the final supplies have been unloaded from the ships. Supplies are stored in stacks of marked crates, but because the needed supplies are buried under many other crates, the crates need to be rearranged.

The ship has a giant cargo crane capable of moving crates between stacks. To ensure none of the crates get crushed or fall over, the crane operator will rearrange them in a series of carefully-planned steps. After the crates are rearranged, the desired crates will be at the top of each stack.

The Elves don’t want to interrupt the crane operator during this delicate procedure, but they forgot to ask her which crate will end up where, and they want to be ready to unload them as soon as possible so they can embark.

They do, however, have a drawing of the starting stacks of crates and the rearrangement procedure (your puzzle input). For example:

    [D]
[N] [C]
[Z] [M] [P]
 1   2   3

move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2
In this example, there are three stacks of crates. Stack 1 contains two crates: crate Z is on the bottom, and crate N is on top. Stack 2 contains three crates; from bottom to top, they are crates M, C, and D. Finally, stack 3 contains a single crate, P.

Then, the rearrangement procedure is given. In each step of the procedure, a quantity of crates is moved from one stack to a different stack. In the first step of the above rearrangement procedure, one crate is moved from stack 2 to stack 1, resulting in this configuration:

	[D]
	[N] [C]
	[Z] [M] [P]
	 1   2   3

In the second step, three crates are moved from stack 1 to stack 3. Crates are moved one at a time, so the first crate to be moved (D) ends up below the second and third crates:

        [Z]
        [N]
    [C] [D]
    [M] [P]

 1   2   3

Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved one at a time, crate C ends up below crate M:

	        [Z]
	        [N]
	[M] 	[D]
	[C] 	[P]
	 1   2   3

Finally, one crate is moved from stack 1 to stack 2:

        [Z]
        [N]
        [D]
[C] [M] [P]
 1   2   3

The Elves just need to know which crate will end up on top of each stack; in this example, the top crates are C in stack 1, M in stack 2, and Z in stack 3, so you should combine these together and give the Elves the message CMZ.

After the rearrangement procedure completes, what crate ends up on top of each stack?



use regex;
use std::fs::File;
use std::io::{
   BufRead, BufReader};

fn main() {
   
    let mut stacks = vec![
        vec!['F', 'C', 'P', 'G', 'Q', 'R'],
        vec!['W', 'T', 'C', 'P'],
        vec!['B', 'H', 'P', 'M', 'C'],
        vec!['L', 'T', 'Q', 'S', 'M', 'P', 'R'],
        vec!['P', 'H', 'J', 'Z', 'V', 'G', 'N'],
        vec!['D', 'P', 'J'],
        vec!['L', 'G'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值