sentry's recent activity

  1. Comment on Day 2: Dive! in ~comp

    sentry
    Link
    I'm late but I wanted to give it a try anyway! Part 1 (Rust) fn main() { let mut x = 0; let mut y = 0; include_str!("../../input") .lines() .map(|line| line.split_once(' ').unwrap()...

    I'm late but I wanted to give it a try anyway!

    Part 1 (Rust)
    fn main() {
        let mut x = 0;
        let mut y = 0;
        include_str!("../../input")
            .lines()
            .map(|line| line.split_once(' ').unwrap()
            .for_each(|cmd| match cmd.0.chars().nth(0).unwrap()
                'u' => y = y - cmd.1.parse::<i32>().unwrap()
                'd' => y = y + cmd.1.parse::<i32>().unwrap()
                'f' => x = x + cmd.1.parse::<i32>().unwrap()
                _ => panic!("Invalid direction"),
            });
        println!("position = {}", x * y);
    }
    
    Part 2 (Rust)
    fn main() {
        let mut aim = 0;
        let mut x = 0;
        let mut y = 0;
        include_str!("../../input")
            .lines()
            .map(|line| line.split_once(' ').unwrap()
            .for_each(|cmd| match cmd.0.chars().nth(0).unwrap()
                'u' => aim = aim - cmd.1.parse::<i32>().unwrap()
                'd' => aim = aim + cmd.1.parse::<i32>().unwrap()
                'f' => {
                    x = x + cmd.1.parse::<i32>().unwrap()
                    y = y + aim * cmd.1.parse::<i32>().unwrap()
                }
                _ => panic!("Invalid direction"),
            });
        println!("position = {}", x * y);
    }
    
    2 votes