use std::env::args; use std::fs::File; use std::io::BufRead; use std::io::BufReader; fn main() { let path = args().nth(1).unwrap(); let file = File::open(path).unwrap(); let mut x = 0; let mut y = 0; let mut aim = 0; for line in BufReader::new(file).lines() { let l = line.unwrap(); let i = l.find(' ').unwrap(); let dir = &l[..i]; let k = &l[i+1..].parse::().unwrap(); match dir { "down" => aim += k, "up" => aim -= k, "forward" => { x += k; y += k * aim; }, _ => panic!("invalid direction"), } } print!("{}\n", x * y); }