|
|
@@ -0,0 +1,86 @@
|
|
|
+use std::fs::File;
|
|
|
+use std::io::{BufReader,BufRead};
|
|
|
+
|
|
|
+fn main() {
|
|
|
+ let filename = "input.txt";
|
|
|
+ let file = File::open(filename).unwrap();
|
|
|
+ let reader = BufReader::new(file);
|
|
|
+ //ugly beacuse fuck rust and its weird array inits
|
|
|
+ let mut containers = [Vec::new(),Vec::new(),Vec::new(),Vec::new(),Vec::new(),Vec::new(),Vec::new(),Vec::new(),Vec::new()];
|
|
|
+ for (index,line) in reader.lines().enumerate(){
|
|
|
+ let line = line.unwrap();
|
|
|
+ //parse containers
|
|
|
+ if index < 8 {
|
|
|
+ let mut charpos = 0;
|
|
|
+ for c in line.chars(){
|
|
|
+ charpos += 1;
|
|
|
+ match c{
|
|
|
+ '[' | ']' | ' ' => continue,
|
|
|
+ _ => {
|
|
|
+ let pos = (charpos + 2) / 4;
|
|
|
+ containers[pos - 1].push(c);
|
|
|
+ //println!("{c} - {charpos} - {pos}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if index == 9{
|
|
|
+ fix_containers(&mut containers);
|
|
|
+ print_containers(&containers);
|
|
|
+ }
|
|
|
+ //move containers
|
|
|
+ if index > 9{
|
|
|
+ let parts: Vec<&str> = line.split(' ').collect();
|
|
|
+ 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);
|
|
|
+ println!("{line}");
|
|
|
+ print_containers(&containers);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ let mut top: String = String::new();
|
|
|
+ for mut v in containers{
|
|
|
+ top.push(v.pop().unwrap());
|
|
|
+ }
|
|
|
+ println!("{top}");
|
|
|
+}
|
|
|
+
|
|
|
+fn print_containers(con: &[Vec<char>;9]){
|
|
|
+ println!("");
|
|
|
+ println!("");
|
|
|
+ for i in 0..9{
|
|
|
+ let mut line: String = String::new();
|
|
|
+ line.push_str(&(i+1).to_string());
|
|
|
+ for c in &con[i]{
|
|
|
+ line.push(*c);
|
|
|
+ }
|
|
|
+ println!("{line}");
|
|
|
+ }
|
|
|
+ println!("--------------------------------------------");
|
|
|
+}
|
|
|
+//apparenty move is a keyword and we can escape those with r# dunno how im ganna call this
|
|
|
+fn r#move(ammount: i32,from: usize, to: usize, containers: &mut [Vec<char>;9]){
|
|
|
+ for i in 0..ammount{
|
|
|
+ let crane: char = containers[from - 1].pop().unwrap();
|
|
|
+ containers[to - 1].push(crane);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+fn fix_containers(con: &mut [Vec<char>;9]){
|
|
|
+ for i in 0..9{
|
|
|
+ let mut tmp = con[i].clone();
|
|
|
+ let tlen = tmp.len();
|
|
|
+ let clen = con[i].len();
|
|
|
+ println!("fixin tmp with length {tlen} form con with len {clen}");
|
|
|
+ con[i] = Vec::new();
|
|
|
+ for j in 0..tmp.len() {
|
|
|
+ let movin = tmp.pop().unwrap();
|
|
|
+ println!("{movin} - {j}");
|
|
|
+ con[i].push(movin);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|