Advent Of Code Input Parsing

under Programming Rust Advent of Code

 

With 2021 done, wanted to go back and put together a gist list of the input parsing, using standard crate Rust, for future reference

List of numbers

Day 1

Easy enough, leverage parse::<type> turbo fish for your correct type size:

    let stdin = io::stdin();
    let mut numbers: Vec<i32> = Vec::new();
    for line in stdin.lock().lines() {
        numbers.push(line.unwrap().parse::<i32>().unwrap());
    }

List of commands

Day 2, Day 13 (element 2), Day 21. These vary a bit, based upon the command structure, number of options, but effectivly use split() or replacen() for each line.

Below is an example of two-part command, direction and magnituded, separated by space:

    let stdin = io::stdin();
    let lines: Vec<String> = stdin.lock().lines().flatten().collect();

    lines.iter().for_each(|line| {
        let mut command = line.split_whitespace();
        let direction = command.next().unwrap();
        let magnitude = command.next().unwrap().parse::<i32>().unwrap();
        match direction {
            "forward" => horizontal += magnitude,
            "up" => depth -= magnitude,
            "down" => depth += magnitude,
            _ =>  println!("Throw the switch Vern, she's pumping mud")
        }
    });

List of bits

Day 3. Bit packed integers

    let stdin = io::stdin();
    let bits: Vec<u32> = stdin.lock().lines().flatten()
                            .flat_map(|bstr| u32::from_str_radix(&bstr, 2)).collect();

List of coordinates

List of coordinates: Day 13 (element 1), Day 19. Pretty common to leverage a struct, called Point for storing:

    let stdin = io::stdin();
    let lines: Vec<String> = stdin.lock().lines().flatten().collect();
    let mut points: Vec<Point> = lines.iter().filter_map(|line| {
                               match line.contains(",") {
                                   false => None,
                                   true => {
                                        let mut cord = line.split(",");
                                        let x = cord.next().unwrap().parse::<u16>().unwrap();
                                        let y = cord.next().unwrap().parse::<u16>().unwrap();
                                        return Some(Point { x: x, y: y});
                                   }
                               }
                            }).collect();

Single-line CSV of numbers

Day 4 (element 1), Day 6, Day 7

    let start = Instant::now();
    let stdin = io::stdin();
    let mut numbers: Vec<i32> = stdin.lock().lines().next().unwrap().unwrap().split(",").flat_map(|s| s.trim().parse::<i32>()).collect();

Matrix of numbers/characters

Day 4 (element 2), Day 9, Day 11, Day 15, Day 20 (element 2). Generally create single dim array and leverage calculation for row/column offsets.

Below is a matrix of characters:

    let stdin = io::stdin();
    let map: Vec<Vec<char>> = stdin.lock().lines().map(|line| line.unwrap().chars().collect::<Vec<char>>()).collect();

List of pair of string sequences/expressions

Day 8, Day 10, Day 12, Day 14 (element 2), Day 18. These are pretty specific to the implementation, but vary from above

Single line sequence/expression

Day 14 (element 1), Day 16, Day 17, Day 20 (element 1). Same. These are pretty specific to the implementation, but vary from above

Multi-element

Day 4, Day 13, Day 14, Day 19, Day 20. Just combinations of the above