@@ 6,12 6,18 @@ struct Crate {
name: char,
}
-struct Stack {
- crates: Vec<Crate>,
-}
-
-struct State {
- stacks: Vec<Stack>,
+fn build_stacks(initial_state_lines: Vec<String>) -> Vec<VecDeque<Crate>> {
+ let mut stacks: Vec<VecDeque<Crate>> = vec![VecDeque::new(); 9];
+ for (_, line) in initial_state_lines.iter().enumerate() {
+ let crate_names = parse_line(line.to_string());
+ for (i, crate_name) in crate_names.iter().enumerate() {
+ if crate_name.is_alphabetic() {
+ let xmas_crate = Crate { name: *crate_name };
+ stacks[i].push_back(xmas_crate);
+ }
+ }
+ }
+ stacks
}
fn parse_line(line: String) -> Vec<char> {
@@ 34,39 40,51 @@ fn parse_move(line: String) -> (i32, usize, usize) {
}
fn part1() {
- let mut stacks: Vec<VecDeque<Crate>> = vec![VecDeque::new(); 9];
- let lines = read_input("./input.txt");
- for (idx, line) in lines.iter().enumerate() {
- if idx < 8 {
- let crate_names = parse_line(line.to_string());
- for (i, crate_name) in crate_names.iter().enumerate() {
- if crate_name.is_alphabetic() {
- let xmas_crate = Crate { name: *crate_name };
- stacks[i].push_back(xmas_crate);
- }
- }
- }
- if idx >= 10 {
- let (crate_count, from_stack_i, to_stack_i) = parse_move(line.to_string());
- for _ in 0..crate_count {
- let moving_crate = stacks[from_stack_from_stacki - 1].pop_front().unwrap();
- stacks[to_stack_i - 1].push_front(moving_crate);
- }
+ let (initial_state_lines, instructions_lines) = read_input("./input.txt");
+ let mut stacks = build_stacks(initial_state_lines);
+ for (_, line) in instructions_lines.iter().enumerate() {
+ let (crate_count, from_stack_i, to_stack_i) = parse_move(line.to_string());
+ for _ in 0..crate_count {
+ let moving_crate = stacks[from_stack_i - 1].pop_front().unwrap();
+ stacks[to_stack_i - 1].push_front(moving_crate);
}
}
for stack in stacks {
- println!("top crate in stack {:?}", stack[0]);
+ println!("Part1: top crate in stack {:?}", stack[0].name);
}
}
-fn part2() {}
-
+fn part2() {
+ let (initial_state_lines, instructions_lines) = read_input("./input.txt");
+ let mut stacks = build_stacks(initial_state_lines);
+ for (_, line) in instructions_lines.iter().enumerate() {
+ let (crate_count, from_stack_i, to_stack_i) = parse_move(line.to_string());
+ let mut moving_crates = vec![];
+ for _ in 0..crate_count {
+ let moving_crate = stacks[from_stack_i - 1].pop_front().unwrap();
+ moving_crates.push(moving_crate);
+ }
+ moving_crates.reverse();
+ for moving_crate in moving_crates {
+ stacks[to_stack_i - 1].push_front(moving_crate);
+ }
+ }
+ for stack in stacks {
+ println!("Part2: top crate in stack {:?}", stack[0].name);
+ }
+}
fn main() {
part1();
part2();
}
-fn read_input(file_name: &str) -> Vec<String> {
+fn read_input(file_name: &str) -> (Vec<String>, Vec<String>) {
let s = fs::read_to_string(file_name).unwrap();
- s.lines().map(|l| l.to_string()).collect::<Vec<_>>()
+ let initial_state_lines = s.lines().take(8).map(|l| l.to_string()).collect::<Vec<_>>();
+ let instructions_lines = s
+ .lines()
+ .skip(10)
+ .map(|l| l.to_string())
+ .collect::<Vec<_>>();
+ (initial_state_lines, instructions_lines)
}