Refactor to use common lib
Added solution for AoC 2022/2 Signed-off-by: TuDatTr <tuan-dat.tran@tudattr.dev>
This commit is contained in:
8
2022/day-1_calorie_counting/Cargo.lock
generated
8
2022/day-1_calorie_counting/Cargo.lock
generated
@@ -57,11 +57,19 @@ dependencies = [
|
||||
"os_str_bytes",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "common"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day-1_calorie_counting"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"common",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
@@ -19,4 +19,5 @@ path = "src/bin.rs"
|
||||
[dependencies]
|
||||
clap = {version = "4.0.22", features = ["derive"]}
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = {version = "0.3.16", features = ["env-filter"]}
|
||||
tracing-subscriber = {version = "0.3.16", features = ["env-filter"]}
|
||||
common = {path = "../common/"}
|
||||
@@ -2,7 +2,7 @@ use clap::Parser;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about="CLI tool to output latency at a given gps location.", long_about = None)]
|
||||
#[command(author, version, about=None, long_about = None)]
|
||||
pub struct Cli {
|
||||
/// Path to the input file.
|
||||
#[arg(short, long)]
|
||||
|
||||
@@ -1,35 +1,14 @@
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::PathBuf;
|
||||
use std::collections::BinaryHeap;
|
||||
use tracing::{debug, info};
|
||||
use common::read_file;
|
||||
|
||||
mod elf;
|
||||
use crate::elf::Elf;
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn create_file(path: &Path, content: String) {
|
||||
info!("Creating file");
|
||||
debug!("Writing file from: {:?}\nWith:\n{}", path, content);
|
||||
|
||||
let mut file = File::create(path).unwrap();
|
||||
let _ = file.write_all(content.as_bytes());
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn read_file(path: &Path) -> std::io::Result<String> {
|
||||
info!("Reading file");
|
||||
debug!("Reading file from: {:?}", path);
|
||||
|
||||
let mut file = File::open(path)?;
|
||||
let mut contents = String::new();
|
||||
let _ = file.read_to_string(&mut contents)?;
|
||||
Ok(contents.trim().to_string())
|
||||
}
|
||||
|
||||
/// Finds the Elf carrying the most Calories.
|
||||
/// Outputs how many total Calories that Elf is carrying.
|
||||
#[allow(dead_code)]
|
||||
pub fn task_1a(input: &PathBuf) {
|
||||
debug!("Running task 1a");
|
||||
info!("Running task 1a with {:?}", input);
|
||||
@@ -41,7 +20,6 @@ pub fn task_1a(input: &PathBuf) {
|
||||
|
||||
/// Finds the top three Elves carrying the most Calories.
|
||||
/// Outputs how many Calories those Elves are carrying in total.
|
||||
#[allow(dead_code)]
|
||||
pub fn task_1b(input: &PathBuf) {
|
||||
debug!("Running task 1b");
|
||||
info!("Running task 1b with {:?}", input);
|
||||
@@ -64,25 +42,26 @@ mod tests {
|
||||
use super::*;
|
||||
use std::fs::remove_file;
|
||||
use std::str::from_utf8;
|
||||
use common::{create_file, read_file};
|
||||
|
||||
const PATH: &str = "input.txt";
|
||||
const CONTENT: &[u8; 55] =
|
||||
b"1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000\n";
|
||||
const CONTENT: &[u8; 54] =
|
||||
b"1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000";
|
||||
|
||||
fn create_test_setup() {
|
||||
let file_path = Path::new(PATH);
|
||||
let file_path = &PathBuf::from(PATH);
|
||||
let original = CONTENT;
|
||||
create_file(file_path, from_utf8(original).unwrap().to_string());
|
||||
}
|
||||
|
||||
fn cleanup_test_setup() {
|
||||
let _ = remove_file(Path::new(PATH));
|
||||
let _ = remove_file(&PathBuf::from(PATH));
|
||||
}
|
||||
/// Test if we corretly read file input.
|
||||
#[test]
|
||||
fn file_io() {
|
||||
create_test_setup();
|
||||
let content = read_file(Path::new(PATH));
|
||||
let content = read_file(&PathBuf::from(PATH));
|
||||
cleanup_test_setup();
|
||||
assert_eq!(from_utf8(CONTENT).unwrap(), content.unwrap());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user