initial commit

This commit is contained in:
Daniel Yrovas 2022-11-10 16:58:11 +11:00
commit 658f3bfca5
Signed by: danielyrovas
GPG key ID: C181BAC70BDE7C00
31 changed files with 7570 additions and 0 deletions

33
02-dive!/src/main.rs Normal file
View file

@ -0,0 +1,33 @@
#![allow(dead_code, unused_imports)]
use std::fs::File;
use std::path::Path;
use std::io::{self, BufRead};
fn main() {
let mut h = 0;
let mut v = 0;
let mut aim = 0;
if let Ok(lines) = read_lines("directions.txt") {
for line in lines {
if let Ok(val) = line {
println!("{}", val);
//shadow assignment
let val: Vec<&str> = val.split(' ').collect();
let cmd = val[0];
let n = val[1].parse::<u32>().unwrap();
match cmd {
"forward" => {h += n; v += n * aim },
"down" => aim += n,
"up" => aim -= n,
_ => println!("not a command"),
}
}
}
}
println!("h: {}, v: {}, final position: {}", h, v, h * v );
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> where P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}