nairby

2022 Day 05

Dec 5th, 2022 (edited)
1,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.80 KB | None | 0 0
  1. use std::env;
  2. use std::io::{self};
  3.  
  4. extern crate regex;
  5. use regex::Regex;
  6.  
  7. #[macro_use] extern crate lazy_static;
  8. lazy_static! {
  9.     static ref RE_MOVES: Regex = {
  10.         Regex::new(r"move ([\d]+) from ([\d]+) to ([\d]+)").unwrap()
  11.     };
  12. }
  13.  
  14. #[derive(Debug)]
  15. struct Move {
  16.     qty: usize,
  17.     from: usize,
  18.     to: usize,
  19. }
  20. impl From<&str> for Move {
  21.     fn from(s: &str) -> Self {
  22.         let matches = RE_MOVES.captures(s).unwrap();
  23.         Self {
  24.             qty:  matches[1].parse().unwrap(),
  25.             from: matches[2].parse().unwrap(),
  26.             to:   matches[3].parse().unwrap(),
  27.         }
  28.     }
  29. }
  30.  
  31. const MAX_NUM_STACKS: usize = 9;
  32.  
  33. fn move_last_n_chars(s1: &str, s2: &str, n: usize) -> (String, String) {
  34.     let mut s1_chars: Vec<char> = s1.chars().collect();
  35.     let     s2_chars: Vec<char> = s2.chars().collect();
  36.    
  37.     let mut moved_chars: String = s1_chars.iter().rev().take(n).collect();
  38.     moved_chars = moved_chars.chars().rev().collect();
  39.  
  40.     for _ in 0..n { s1_chars.pop().unwrap(); }
  41.  
  42.     let s1_new = s1_chars.into_iter().collect();
  43.     let mut s2_new: String = s2_chars.into_iter().collect();
  44.     s2_new.push_str(&moved_chars);
  45.  
  46.     (s1_new, s2_new)
  47. }
  48.  
  49. fn solve(input: &str) -> io::Result<()> {
  50.     let input_str = std::fs::read_to_string(input).unwrap();
  51.     let input_str = input_str.trim();
  52.     let input: Vec<_> = input_str.split("\n\n").collect();
  53.  
  54.     // Initialize moves
  55.     let moves: Vec<_> = input[1].split("\n").map(Move::from).collect();
  56.  
  57.     // Initialize stacks
  58.     let mut stacks: [String; MAX_NUM_STACKS] = Default::default();
  59.     for line in input[0].lines() {
  60.         if line.chars().nth(1).unwrap() == '1' { continue } // Skip last line indexing the stacks
  61.         for (ix,ch) in line.chars().enumerate() {
  62.             match ix {
  63.                 1  => if ch != ' ' { stacks[0].push_str(&ch.to_string()) },
  64.                 5  => if ch != ' ' { stacks[1].push_str(&ch.to_string()) },
  65.                 9  => if ch != ' ' { stacks[2].push_str(&ch.to_string()) },
  66.                 13 => if ch != ' ' { stacks[3].push_str(&ch.to_string()) },
  67.                 17 => if ch != ' ' { stacks[4].push_str(&ch.to_string()) },
  68.                 21 => if ch != ' ' { stacks[5].push_str(&ch.to_string()) },
  69.                 25 => if ch != ' ' { stacks[6].push_str(&ch.to_string()) },
  70.                 29 => if ch != ' ' { stacks[7].push_str(&ch.to_string()) },
  71.                 33 => if ch != ' ' { stacks[8].push_str(&ch.to_string()) },
  72.                 _  => {},
  73.             }
  74.         }
  75.     }
  76.  
  77.     // Reverse the stacks
  78.     for ix in 0..MAX_NUM_STACKS {
  79.         stacks[ix] = stacks[ix].chars().rev().collect::<String>();
  80.     }
  81.    
  82.     // Copy original state for part 2
  83.     let stacks_copy = stacks.clone();
  84.  
  85.     // Process part 1 moves
  86.     for moved in &moves {
  87.         let (qty,from,to) = (moved.qty,moved.from-1, moved.to-1);
  88.         for _ in 0..qty {
  89.             //(stacks[from],stacks[to]) = move_last_char(&stacks[from],&stacks[to]);
  90.             (stacks[from],stacks[to]) = move_last_n_chars(&stacks[from],&stacks[to],1);
  91.         }
  92.     }
  93.  
  94.     // Part 1 Output
  95.     print!("Part 1: "); // VGBBJCRMN
  96.     for ix in 0..MAX_NUM_STACKS { print!("{}",stacks[ix].chars().last().unwrap()); }
  97.     println!();
  98.  
  99.     // Process part 2 moves
  100.     stacks = stacks_copy;
  101.     for moved in &moves {
  102.         let (qty,from,to) = (moved.qty,moved.from-1, moved.to-1);
  103.         (stacks[from],stacks[to]) = move_last_n_chars(&stacks[from],&stacks[to],qty);
  104.     }
  105.  
  106.     // Part 2 Output
  107.     print!("Part 2: "); // LBBVJBRMH
  108.     for ix in 0..MAX_NUM_STACKS { print!("{}",stacks[ix].chars().last().unwrap()); }
  109.     println!();
  110.  
  111.     Ok(())
  112. }
  113.  
  114. fn main() {
  115.     let args: Vec<String> = env::args().collect();
  116.     let filename = &args[1];
  117.     solve(&filename).unwrap();
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment