Add aoc crate that reads file from input argument; use it in the day01 crate

This commit is contained in:
Eryn Wells 2022-12-19 19:26:10 +00:00
parent 545463c40c
commit 4710909b98
6 changed files with 34 additions and 7 deletions

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

8
2022/aoc/Cargo.toml Normal file
View file

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

8
2022/aoc/src/lib.rs Normal file
View file

@ -0,0 +1,8 @@
use std::{env, fs};
pub fn read_input_file_to_string() -> String {
let args: Vec<String> = env::args().collect();
let filename = args.get(1).expect("Missing filename argument");
fs::read_to_string(&filename).expect("Unable to read file")
}

7
2022/day01/Cargo.lock generated
View file

@ -2,6 +2,13 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc"
version = "0.1.0"
[[package]]
name = "day1"
version = "0.1.0"
dependencies = [
"aoc",
]

View file

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
aoc = { path = "../aoc" }

View file

@ -1,5 +1,4 @@
use std::env;
use std::fs;
use aoc;
use std::str::Lines;
fn get_calorie_totals(lines: Lines) -> Vec<u32> {
@ -23,12 +22,9 @@ fn get_calorie_totals(lines: Lines) -> Vec<u32> {
}
fn main() {
let args: Vec<String> = env::args().collect();
let filename = args.get(1).expect("Missing filename argument");
let file_contents = fs::read_to_string(&filename).expect("Unable to read file");
let file_contents = aoc::read_input_file_to_string();
let lines = file_contents.lines();
let elves = get_calorie_totals(lines);
println!(