day5: both parts
This commit is contained in:
4 files changed
+119
-2
No files matched your search
Binary file not shown.
|
Before Width: | Height: | Size: 4.4 KiB After Width: | Height: | Size: 5.5 KiB |
@@ -1,6 +1,6 @@
|
|||||||
<!-- AOC TILES BEGIN -->
|
<!-- AOC TILES BEGIN -->
|
||||||
<h1 align="center">
|
<h1 align="center">
|
||||||
2022 - 12 ⭐ - Rust
|
2022 - 14 ⭐ - Rust
|
||||||
</h1>
|
</h1>
|
||||||
<a href="src/day1.rs">
|
<a href="src/day1.rs">
|
||||||
<img src=".aoc_tiles/tiles/2022/01.png" width="200">
|
<img src=".aoc_tiles/tiles/2022/01.png" width="200">
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
<a href="src/day4.rs">
|
<a href="src/day4.rs">
|
||||||
<img src=".aoc_tiles/tiles/2022/04.png" width="200">
|
<img src=".aoc_tiles/tiles/2022/04.png" width="200">
|
||||||
</a>
|
</a>
|
||||||
<a href="None">
|
<a href="src/day5.rs">
|
||||||
<img src=".aoc_tiles/tiles/2022/05.png" width="200">
|
<img src=".aoc_tiles/tiles/2022/05.png" width="200">
|
||||||
</a>
|
</a>
|
||||||
<a href="src/day6.rs">
|
<a href="src/day6.rs">
|
||||||
|
|||||||
+116
@@ -0,0 +1,116 @@
|
|||||||
|
use std::collections::{HashMap, VecDeque};
|
||||||
|
|
||||||
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Instruction {
|
||||||
|
n: usize,
|
||||||
|
from: usize,
|
||||||
|
to: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_machine(stacks: &mut Vec<VecDeque<u8>>, instructions: &Vec<Instruction>) {
|
||||||
|
for inst in instructions {
|
||||||
|
for _ in 0..inst.n {
|
||||||
|
let moving = stacks[inst.from].pop_front().unwrap();
|
||||||
|
stacks[inst.to].push_front(moving);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_machine2(stacks: &mut Vec<VecDeque<u8>>, instructions: &Vec<Instruction>) {
|
||||||
|
for inst in instructions {
|
||||||
|
let mut moving = stacks[inst.from].split_off(inst.n);
|
||||||
|
// semantics of split_off are opposite of what we want
|
||||||
|
std::mem::swap(&mut moving, &mut stacks[inst.from]);
|
||||||
|
moving.append(&mut stacks[inst.to]);
|
||||||
|
stacks[inst.to] = moving;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc_generator(day5)]
|
||||||
|
fn parse(input: &str) -> (Vec<VecDeque<u8>>, Vec<Instruction>) {
|
||||||
|
enum State {
|
||||||
|
Stacks,
|
||||||
|
Ids,
|
||||||
|
Instructions,
|
||||||
|
}
|
||||||
|
let mut stacks = HashMap::new();
|
||||||
|
let mut instructions = Vec::new();
|
||||||
|
let mut state = State::Stacks;
|
||||||
|
for line in input.lines() {
|
||||||
|
match state {
|
||||||
|
State::Stacks => {
|
||||||
|
for (stack, top) in line.as_bytes().chunks(4).into_iter().map(|s| s[1]).enumerate() {
|
||||||
|
if top.is_ascii_digit() {
|
||||||
|
state = State::Ids
|
||||||
|
} else if top != b' ' {
|
||||||
|
stacks.entry(stack).or_insert(VecDeque::new()).push_back(top);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
State::Ids => {
|
||||||
|
if line == "" {
|
||||||
|
state = State::Instructions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
State::Instructions => {
|
||||||
|
let tokens = line.split_ascii_whitespace().collect_vec();
|
||||||
|
|
||||||
|
instructions.push(Instruction {
|
||||||
|
n: tokens[1].parse::<usize>().unwrap(),
|
||||||
|
from: tokens[3].parse::<usize>().unwrap() - 1,
|
||||||
|
to: tokens[5].parse::<usize>().unwrap() - 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(
|
||||||
|
stacks
|
||||||
|
.iter()
|
||||||
|
.sorted_by_key(|(k, _v)| **k)
|
||||||
|
.map(|(_k, v)| v.clone())
|
||||||
|
.collect_vec(),
|
||||||
|
instructions,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day5, part1)]
|
||||||
|
fn part1((stacks, instructions): &(Vec<VecDeque<u8>>, Vec<Instruction>)) -> String {
|
||||||
|
let mut stacks = stacks.clone();
|
||||||
|
run_machine(&mut stacks, &instructions);
|
||||||
|
stacks.iter().map(|s| *s.front().unwrap() as char).join("")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day5, part2)]
|
||||||
|
fn part2((stacks, instructions): &(Vec<VecDeque<u8>>, Vec<Instruction>)) -> String {
|
||||||
|
let mut stacks = stacks.clone();
|
||||||
|
run_machine2(&mut stacks, &instructions);
|
||||||
|
stacks.iter().map(|s| *s.front().unwrap() as char).join("")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
const EXAMPLE: &str = " [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";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_example() {
|
||||||
|
assert_eq!(part1(&parse(EXAMPLE)), "CMZ");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_example() {
|
||||||
|
assert_eq!(part2(&parse(EXAMPLE)), "MCD");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
mod day5;
|
||||||
mod day7;
|
mod day7;
|
||||||
mod day4;
|
mod day4;
|
||||||
mod day1;
|
mod day1;
|
||||||
|
|||||||
Reference in new issue
Block a user