Browse Source

day 5 part 2

Phil 2 years ago
parent
commit
484ffe7a9b
1 changed files with 17 additions and 1 deletions
  1. 17 1
      aoc05/src/main.rs

+ 17 - 1
aoc05/src/main.rs

@@ -1,7 +1,9 @@
+use std::env;
 use std::fs::File;
 use std::io::{BufReader,BufRead};
 
 fn main() {
+    let args: Vec<String> = env::args().collect();
     let filename = "input.txt";
     let file = File::open(filename).unwrap();
     let reader = BufReader::new(file);
@@ -34,7 +36,11 @@ fn main() {
             let ammount = parts[1].parse::<i32>().unwrap();
             let from = parts[3].parse::<usize>().unwrap();
             let to = parts[5].parse::<usize>().unwrap();
-            r#move(ammount,from,to,&mut containers);
+            if args[1] == "1"{
+                r#move(ammount,from,to,&mut containers);
+            }else{
+                r#move_as_stack(ammount,from,to,&mut containers);
+            }
             println!("{line}");
             print_containers(&containers);
         }
@@ -69,6 +75,16 @@ fn r#move(ammount: i32,from: usize, to: usize, containers: &mut [Vec<char>;9]){
 
 }
 
+fn move_as_stack(ammount: i32,from: usize, to: usize, containers: &mut [Vec<char>;9]){
+    let mut crane: Vec<char> = Vec::new();
+    for i in 0..ammount{
+        crane.push(containers[from - 1].pop().unwrap());
+    }
+
+    for i in 0..ammount{
+        containers[to - 1].push(crane.pop().unwrap());
+    }
+}
 
 fn fix_containers(con: &mut [Vec<char>;9]){
     for i in 0..9{