Part 1
This commit is contained in:
parent
3d926bfdb1
commit
d46fd97fd9
|
@ -1,25 +1,95 @@
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
type Input = String;
|
#[derive(Debug)]
|
||||||
type Result = usize;
|
struct Move {
|
||||||
|
amount: i32,
|
||||||
fn processed_input(input: &str) -> Input {
|
from: usize,
|
||||||
input.to_owned()
|
to: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(input: &Input) -> Result {
|
#[derive(Debug)]
|
||||||
100
|
struct Crates {
|
||||||
|
stacks: Vec<Vec<char>>,
|
||||||
|
moves: Vec<Move>,
|
||||||
|
}
|
||||||
|
|
||||||
|
const NUM_STACKS: usize = 9;
|
||||||
|
|
||||||
|
type Input = Crates;
|
||||||
|
type Result = String;
|
||||||
|
|
||||||
|
fn processed_input(input: &str) -> Input {
|
||||||
|
let mut stacks: Vec<Vec<char>> = vec![];
|
||||||
|
let mut moves: Vec<Move> = vec![];
|
||||||
|
|
||||||
|
for _ in 0..NUM_STACKS {
|
||||||
|
stacks.push(vec![]);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut parsing_stacks = true;
|
||||||
|
for l in input.lines() {
|
||||||
|
if l.trim() == "" || l.chars().skip(1).next().unwrap() == '1' {
|
||||||
|
parsing_stacks = false;
|
||||||
|
continue;
|
||||||
|
} else if parsing_stacks {
|
||||||
|
let mut x = 0;
|
||||||
|
for c in l.chars().skip(1).step_by(4) {
|
||||||
|
x += 1;
|
||||||
|
if c == ' ' {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
stacks[x - 1].push(c);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let words: Vec<&str> = l.split_whitespace().collect();
|
||||||
|
println!("words: {:?}", words);
|
||||||
|
moves.push(Move {
|
||||||
|
amount: words[1].parse().unwrap(),
|
||||||
|
from: words[3].parse::<usize>().unwrap() - 1,
|
||||||
|
to: words[5].parse::<usize>().unwrap() - 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for s in &mut stacks[..] {
|
||||||
|
s.reverse();
|
||||||
|
}
|
||||||
|
|
||||||
|
Crates { stacks, moves }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn perform_moves(crates: &mut Crates) {
|
||||||
|
let stacks = &mut crates.stacks;
|
||||||
|
for Move { amount, from, to } in &crates.moves {
|
||||||
|
println!("move: {} {} {}", amount, from, to);
|
||||||
|
for _ in 0..*amount {
|
||||||
|
let popped = stacks[*from].pop().unwrap();
|
||||||
|
stacks[*to].push(popped);
|
||||||
|
}
|
||||||
|
println!("{:?}", stacks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(input: &mut Input) -> Result {
|
||||||
|
println!("{:?}", input);
|
||||||
|
perform_moves(input);
|
||||||
|
|
||||||
|
let mut r = String::from("");
|
||||||
|
for s in &input.stacks {
|
||||||
|
r.push(s.last().unwrap_or(&' ').clone())
|
||||||
|
}
|
||||||
|
r.trim().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(input: &Input) -> Result {
|
fn part2(input: &Input) -> Result {
|
||||||
0
|
"".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input_text = common::day_input(5);
|
let input_text = common::day_input(5);
|
||||||
eprintln!("{}\n\nAbove is your input file.\n\n", input_text);
|
eprintln!("{}\n\nAbove is your input file.\n\n", input_text);
|
||||||
let input = processed_input(&input_text);
|
let mut input = processed_input(&input_text);
|
||||||
common::show_answers(&part1(&input), &part2(&input))
|
common::show_answers(&part1(&mut input), &part2(&input))
|
||||||
// common::show_both_answers(&both_parts(&input))
|
// common::show_both_answers(&both_parts(&input))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,13 +101,22 @@ fn main() {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
const TEST_INPUT: &str = "";
|
const TEST_INPUT: &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]
|
#[test]
|
||||||
fn test() {
|
fn test() {
|
||||||
let input = processed_input(TEST_INPUT);
|
let mut input = processed_input(TEST_INPUT);
|
||||||
assert_eq!(part1(&input), 0);
|
assert_eq!(part1(&mut input), "CMZ");
|
||||||
assert_eq!(part2(&input), 0);
|
let mut input = processed_input(TEST_INPUT);
|
||||||
|
assert_eq!(part2(&input), "");
|
||||||
// assert_eq!(both_parts(&input), (0, 0));
|
// assert_eq!(both_parts(&input), (0, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue