day 2 in rust

This commit is contained in:
Thomas Ruoff
2021-12-03 01:08:14 +01:00
parent 7343b0fe9e
commit 76582a01f7
5 changed files with 1048 additions and 0 deletions

1
day02/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
day02/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 = "day02"
version = "0.1.0"

8
day02/Cargo.toml Normal file
View File

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

1000
day02/data/input Normal file

File diff suppressed because it is too large Load Diff

32
day02/src/main.rs Normal file
View File

@@ -0,0 +1,32 @@
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn main() {
let mut depth = 0;
let mut pos_h = 0;
if let Ok(lines) = read_lines("./data/input") {
for line in lines {
if let Ok(str) = line {
let words: Vec<&str> = str.split_whitespace().collect();
let direction = words[0];
let number = words[1].parse::<i32>().unwrap();
match direction {
"down" => depth = depth + number,
"up" => depth = depth - number,
"forward" => pos_h = pos_h + number,
_ => eprintln!("UNKNOWN!!!!!"),
}
}
}
}
println!("{}", depth * pos_h);
}
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())
}