35 lines
927 B
Rust
35 lines
927 B
Rust
use std::fs::File;
|
|
use std::io::{BufReader};
|
|
use std::io::prelude::*;
|
|
|
|
fn main(){
|
|
let file = File::open("./input.txt").expect("Read failed");
|
|
let reader = BufReader::new(file);
|
|
|
|
let mut current_depth = 0;
|
|
let mut current_horizontal = 0;
|
|
|
|
let all_lines = reader.lines();
|
|
|
|
|
|
for line in all_lines{
|
|
let line_as_string = line.unwrap();
|
|
let split_line = line_as_string.split_whitespace().collect::<Vec<&str>>();
|
|
let direction = split_line[0];
|
|
let distance = split_line[1].parse::<i32>().unwrap();
|
|
|
|
match direction {
|
|
"forward" => current_horizontal += distance,
|
|
"down" => current_depth += distance,
|
|
"up" => current_depth -= distance,
|
|
_ => println!("You should never be here")
|
|
}
|
|
}
|
|
|
|
|
|
|
|
println!("Depth: {}, Horizontal: {}, Result: {}", current_depth, current_horizontal, current_horizontal*current_depth)
|
|
|
|
|
|
|
|
} |