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

1
02-dive!/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
02-dive!/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "dive"
version = "0.1.0"

8
02-dive!/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "dive"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

19
02-dive!/LICENSE Normal file
View file

@ -0,0 +1,19 @@
The MIT License (MIT)
Copyright (c) 2021 danielyrovas
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1000
02-dive!/directions.txt Normal file

File diff suppressed because it is too large Load diff

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())
}