From 4710909b98099b0b6c2525c9775c66dfd6b93686 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Mon, 19 Dec 2022 19:26:10 +0000 Subject: [PATCH] Add aoc crate that reads file from input argument; use it in the day01 crate --- 2022/aoc/Cargo.lock | 7 +++++++ 2022/aoc/Cargo.toml | 8 ++++++++ 2022/aoc/src/lib.rs | 8 ++++++++ 2022/day01/Cargo.lock | 7 +++++++ 2022/day01/Cargo.toml | 1 + 2022/day01/src/main.rs | 10 +++------- 6 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 2022/aoc/Cargo.lock create mode 100644 2022/aoc/Cargo.toml create mode 100644 2022/aoc/src/lib.rs diff --git a/2022/aoc/Cargo.lock b/2022/aoc/Cargo.lock new file mode 100644 index 0000000..a5b53ef --- /dev/null +++ b/2022/aoc/Cargo.lock @@ -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" diff --git a/2022/aoc/Cargo.toml b/2022/aoc/Cargo.toml new file mode 100644 index 0000000..706d2e1 --- /dev/null +++ b/2022/aoc/Cargo.toml @@ -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] diff --git a/2022/aoc/src/lib.rs b/2022/aoc/src/lib.rs new file mode 100644 index 0000000..7b9bd86 --- /dev/null +++ b/2022/aoc/src/lib.rs @@ -0,0 +1,8 @@ +use std::{env, fs}; + +pub fn read_input_file_to_string() -> String { + let args: Vec = env::args().collect(); + let filename = args.get(1).expect("Missing filename argument"); + fs::read_to_string(&filename).expect("Unable to read file") +} + diff --git a/2022/day01/Cargo.lock b/2022/day01/Cargo.lock index ae5bfe8..e0a4e9a 100644 --- a/2022/day01/Cargo.lock +++ b/2022/day01/Cargo.lock @@ -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", +] diff --git a/2022/day01/Cargo.toml b/2022/day01/Cargo.toml index a3c4e52..0503676 100644 --- a/2022/day01/Cargo.toml +++ b/2022/day01/Cargo.toml @@ -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" } diff --git a/2022/day01/src/main.rs b/2022/day01/src/main.rs index 2513bcd..bd0083e 100644 --- a/2022/day01/src/main.rs +++ b/2022/day01/src/main.rs @@ -1,5 +1,4 @@ -use std::env; -use std::fs; +use aoc; use std::str::Lines; fn get_calorie_totals(lines: Lines) -> Vec { @@ -23,12 +22,9 @@ fn get_calorie_totals(lines: Lines) -> Vec { } fn main() { - let args: Vec = 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!(