Added solution for AoC 2022/1

Signed-off-by: TuDatTr <tuan-dat.tran@tudattr.dev>
This commit is contained in:
TuDatTr
2022-12-01 22:17:40 +01:00
parent 86fb084454
commit 9caad95da2
11 changed files with 2914 additions and 9 deletions

View File

@@ -0,0 +1,46 @@
use calorie_counting_lib::*;
use std::path::PathBuf;
use std::collections::BinaryHeap;
use clap::Parser;
use tracing::{info, debug};
use crate::cli::Cli;
mod cli;
/// Finds the Elf carrying the most Calories.
/// Outputs how many total Calories that Elf is carrying.
fn task_1a(input: &PathBuf) {
debug!("Running task 1a");
let file_path = input;
let content = calorie_counting_lib::read_file(file_path).unwrap();
let splits = content.split("\n\n");
println!("{}", splits.map(|s| Elf::new(s).total_calories()).max().unwrap());
}
/// Finds the top three Elves carrying the most Calories.
/// Outputs how many Calories those Elves are carrying in total.
fn task_1b(input: &PathBuf) {
debug!("Running task 1b");
let file_path = input;
let content = calorie_counting_lib::read_file(file_path).unwrap();
let splits = content.split("\n\n");
let mut heap = splits.map(|s| Elf::new(s).total_calories()).collect::<BinaryHeap<_>>();
let mut top_three = Vec::new();
for _ in 0..3 {
if let Some(c) = heap.pop() {
top_three.push(c);
}
}
println!("{}", top_three.iter().sum::<u64>());
}
pub fn main() {
tracing_subscriber::fmt::init();
debug!("Running with debugging level logging");
let args = Cli::parse();
task_1a(&args.input);
task_1b(&args.input);
}

View File

@@ -0,0 +1,10 @@
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)]
pub struct Cli {
/// Path to the input file.
#[arg(short, long)]
pub input: PathBuf,
}

View File

@@ -0,0 +1,91 @@
use std::fs::File;
use std::io::{Write, Read};
use std::path::Path;
use std::fmt;
use tracing::{info, debug};
pub fn create_file(path: &Path, content: String) {
debug!("Creating file");
let mut file = File::create(path).unwrap();
let _ = file.write_all(content.as_bytes());
}
pub fn read_file(path: &Path) -> std::io::Result<String> {
debug!("Reading file");
let mut file = File::open(path)?;
let mut contents = String::new();
let _ = file.read_to_string(&mut contents)?;
Ok(contents.trim().to_string())
}
#[derive(Debug, Clone)]
pub struct Elf {
_input: String,
calories: Vec<u64>,
}
impl Elf {
pub fn new (input: &str) -> Self {
let cal: Vec<u64> = input.split('\n')
.map(|i| i.parse::<u64>().unwrap())
.collect();
Self { _input: input.to_string(), calories: cal }
}
pub fn total_calories(self) -> u64 {
self.calories.iter().sum()
}
}
impl fmt::Display for Elf{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Elf: {:?}", self.calories)
}
}
// pub fn get_calories() -> std::io::Result<Vec<Elf>> {
// let content = read_file(Path::new("input.txt"))?;
// }
#[cfg(test)]
mod tests {
use super::*;
use std::fs::remove_file;
use std::str::from_utf8;
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";
fn create_test_setup() {
let file_path = Path::new(PATH);
let original = CONTENT;
create_file(file_path, from_utf8(original).unwrap().to_string());
}
fn cleanup_test_setup() {
let _ = remove_file(&Path::new(PATH));
}
/// Test if we corretly read file input.
#[test]
fn file_io() {
create_test_setup();
let content = read_file(&Path::new(PATH));
cleanup_test_setup();
assert_eq!(from_utf8(CONTENT).unwrap(), content.unwrap().to_string());
}
/// Test if the calculation of calories each elf is carrying is correct.
#[test]
fn elf_calories() {
create_test_setup();
let content = read_file(&Path::new(PATH)).unwrap();
let file_path = Path::new("input.txt");
let content = calorie_counting_lib::read_file(file_path).unwrap();
let splits = content.split("\n\n");
let result_vec = Vec::<u64>::new();
for elf in splits.map(|s| Elf::new(s)) {
result_vec.push(elf.total_calories());
}
cleanup_test_setup();
}
}

View File

@@ -1,3 +0,0 @@
fn main() {
println!("Hello, world!");
}