JonathanGupton

Advent of Code 2022 - Day 5

Dec 5th, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.49 KB | None | 0 0
  1. fn parse_crates(s: &str) -> Vec<Vec<char>> {
  2.     let n_crates: usize = s
  3.         .lines()
  4.         .last()
  5.         .unwrap()
  6.         .split_whitespace()
  7.         .last()
  8.         .unwrap()
  9.         .parse()
  10.         .unwrap();
  11.  
  12.     let mut crates: Vec<Vec<char>> = Vec::with_capacity(n_crates);
  13.     for _ in 0..n_crates {
  14.         crates.push(Vec::new())
  15.     }
  16.     for row in s.lines().rev() {
  17.         for (i, c) in row.chars().enumerate() {
  18.             if c.is_alphabetic() {
  19.                 let idx = (i - 1) / 4;
  20.                 crates.get_mut(idx).unwrap().push(c);
  21.             }
  22.         }
  23.     }
  24.     crates
  25. }
  26.  
  27. fn parse_instruction(instruction: &str) -> (usize, usize, usize) {
  28.     let mut instruction = instruction.split_whitespace();
  29.     instruction.next();
  30.     let n: usize = instruction.next().unwrap().parse().unwrap();
  31.     instruction.next();
  32.     let source: usize = instruction.next().unwrap().parse().unwrap();
  33.     instruction.next();
  34.     let destination: usize = instruction.next().unwrap().parse().unwrap();
  35.     (n, source, destination)
  36. }
  37.  
  38. fn get_top_crates(crates: Vec<Vec<char>>) -> String {
  39.     String::from_iter(
  40.         crates
  41.             .iter()
  42.             .map(|stack| *stack.iter().last().unwrap())
  43.             .collect::<Vec<char>>(),
  44.     )
  45. }
  46.  
  47. pub fn part_one(input: &str) -> Option<String> {
  48.     let mut split_input = input.split("\r\n\r\n");
  49.     let mut crates = parse_crates(split_input.next().unwrap());
  50.     for instruction in split_input.next().unwrap().lines() {
  51.         let (n, source, destination) = parse_instruction(instruction);
  52.         for _ in 0..n {
  53.             let v = crates.get_mut(source - 1).unwrap().pop().unwrap();
  54.             crates.get_mut(destination - 1).unwrap().push(v);
  55.         }
  56.     }
  57.     let output = get_top_crates(crates);
  58.     Some(output)
  59. }
  60.  
  61. pub fn part_two(input: &str) -> Option<String> {
  62.     let mut split_input = input.split("\r\n\r\n");
  63.     let mut crates = parse_crates(split_input.next().unwrap());
  64.     for instruction in split_input.next().unwrap().lines() {
  65.         let (n, source, destination) = parse_instruction(instruction);
  66.         let mut hold: Vec<char> = Vec::new();
  67.         for _ in 0..n {
  68.             let v = crates.get_mut(source - 1).unwrap().pop().unwrap();
  69.             hold.push(v);
  70.         }
  71.         while !hold.is_empty() {
  72.             let v = hold.pop().unwrap();
  73.             crates.get_mut(destination - 1).unwrap().push(v);
  74.         }
  75.     }
  76.     let output = get_top_crates(crates);
  77.     Some(output)
  78. }
Advertisement
Add Comment
Please, Sign In to add comment