day 01 and day 02
This commit is contained in:
parent
c4a8518269
commit
53df6748d7
|
@ -0,0 +1,12 @@
|
|||
# ---> Rust
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "aoc_01"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,38 @@
|
|||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let file_path = String::from("/home/wafoo/advent_of_code_2022/aoc_01/input");
|
||||
let mut result_vec = Vec::<u32>::new();
|
||||
|
||||
result_vec.push(0);
|
||||
|
||||
println!("In file {}", file_path);
|
||||
|
||||
if let Ok(lines) = read_lines(file_path) {
|
||||
// Consumes the iterator, returns an (Optional) String
|
||||
for line in lines {
|
||||
if let Ok(ip) = line {
|
||||
match ip.as_str() {
|
||||
"" => result_vec.push(0),
|
||||
_ => {
|
||||
let index = result_vec.len() - 1;
|
||||
result_vec[index] += ip.parse::<u32>().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let index = result_vec.len() - 1;
|
||||
result_vec.sort();
|
||||
let result = result_vec[index] + result_vec[index - 1] + result_vec[index - 2];
|
||||
println!("{}", result);
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
# ---> Rust
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "aoc_02"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,38 @@
|
|||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let file_path = String::from("/home/wafoo/advent_of_code_2022/aoc_02/input");
|
||||
let mut result = 0;
|
||||
|
||||
println!("In file {}", file_path);
|
||||
|
||||
if let Ok(lines) = read_lines(file_path) {
|
||||
// Consumes the iterator, returns an (Optional) String
|
||||
for line in lines {
|
||||
if let Ok(ip) = line {
|
||||
match ip.as_str() {
|
||||
"A X" => result += 3 + 0,
|
||||
"A Y" => result += 1 + 3,
|
||||
"A Z" => result += 2 + 6,
|
||||
"B X" => result += 1 + 0,
|
||||
"B Y" => result += 2 + 3,
|
||||
"B Z" => result += 3 + 6,
|
||||
"C X" => result += 2 + 0,
|
||||
"C Y" => result += 3 + 3,
|
||||
"C Z" => result += 1 + 6,
|
||||
_ => result += 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("{}", result);
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
Loading…
Reference in New Issue