mirror of
https://github.com/tomru/adventofcode.git
synced 2026-03-03 06:27:19 +01:00
day 2 in rust
This commit is contained in:
1
day02/.gitignore
vendored
Normal file
1
day02/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
7
day02/Cargo.lock
generated
Normal file
7
day02/Cargo.lock
generated
Normal 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
8
day02/Cargo.toml
Normal 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
1000
day02/data/input
Normal file
File diff suppressed because it is too large
Load Diff
32
day02/src/main.rs
Normal file
32
day02/src/main.rs
Normal 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())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user