diff --git a/2022/Cargo.toml b/2022/Cargo.toml index 6ce7cd8..1b50e57 100644 --- a/2022/Cargo.toml +++ b/2022/Cargo.toml @@ -5,5 +5,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = [] +test_input = [] + [dependencies] anyhow = "1.0.66" diff --git a/2022/src/days/day1.rs b/2022/src/days/day1.rs index 2d41bb5..47fe751 100644 --- a/2022/src/days/day1.rs +++ b/2022/src/days/day1.rs @@ -2,7 +2,12 @@ use anyhow::Result; use std::fs; pub fn run() -> Result<()> { + #[cfg(not(feature = "test_input"))] let file_contents = fs::read_to_string("day1.txt")?; + + #[cfg(feature = "test_input")] + let file_contents = fs::read_to_string("tests/day1.txt")?; + let lines = file_contents.lines(); let mut calories = Vec::with_capacity(lines.clone().count()); let mut count: u32 = 0; diff --git a/2022/src/days/day2.rs b/2022/src/days/day2.rs index 6729ca1..1697a9e 100644 --- a/2022/src/days/day2.rs +++ b/2022/src/days/day2.rs @@ -14,7 +14,12 @@ enum EndStates { } pub fn run() -> Result<()> { + #[cfg(not(feature = "test_input"))] let file_contents = fs::read_to_string("day2.txt")?; + + #[cfg(feature = "test_input")] + let file_contents = fs::read_to_string("tests/day2.txt")?; + let lines = file_contents.lines(); let mut part_one_scores: Vec = Vec::with_capacity(1000); let mut part_two_scores: Vec = Vec::with_capacity(1000); diff --git a/2022/src/days/day3.rs b/2022/src/days/day3.rs index f8f26ff..c4cee1b 100644 --- a/2022/src/days/day3.rs +++ b/2022/src/days/day3.rs @@ -13,7 +13,12 @@ fn get_priority(t: &char) -> u32 { } pub fn run() -> Result<()> { + #[cfg(not(feature = "test_input"))] let file_contents = fs::read_to_string("day3.txt")?; + + #[cfg(feature = "test_input")] + let file_contents = fs::read_to_string("tests/day3.txt")?; + let lines = file_contents.lines(); let mut rucksacks: Vec = Vec::with_capacity(1000); let mut rucksacks2: Vec = Vec::with_capacity(1000); diff --git a/2022/src/days/day4.rs b/2022/src/days/day4.rs index d946abf..9dd8e1e 100644 --- a/2022/src/days/day4.rs +++ b/2022/src/days/day4.rs @@ -2,7 +2,12 @@ use anyhow::Result; use std::fs; pub fn run() -> Result<()> { + #[cfg(not(feature = "test_input"))] let file_contents = fs::read_to_string("day4.txt")?; + + #[cfg(feature = "test_input")] + let file_contents = fs::read_to_string("tests/day4.txt")?; + let lines = file_contents.lines(); let mut part_one_pairs = 0; let mut part_two_pairs = 0; diff --git a/2022/src/days/day5.rs b/2022/src/days/day5.rs index ca62dd8..6b295e0 100644 --- a/2022/src/days/day5.rs +++ b/2022/src/days/day5.rs @@ -2,7 +2,12 @@ use anyhow::Result; use std::fs; pub fn run() -> Result<()> { + #[cfg(not(feature = "test_input"))] let file_contents = fs::read_to_string("day5.txt")?; + + #[cfg(feature = "test_input")] + let file_contents = fs::read_to_string("tests/day5.txt")?; + let mut stacks: Vec> = Vec::with_capacity(10); let mut moves: Vec<(usize, usize, usize)> = Vec::with_capacity(1000); let mut numstacks: usize = 0; diff --git a/2022/src/days/day6.rs b/2022/src/days/day6.rs index f7bd040..dbd9a99 100644 --- a/2022/src/days/day6.rs +++ b/2022/src/days/day6.rs @@ -24,7 +24,13 @@ fn unique_run_position<'a>(line: &'a str, count: usize) -> usize { } pub fn run() -> Result<()> { + #[cfg(not(feature = "test_input"))] let file_contents = fs::read_to_string("day6.txt")?; + + + #[cfg(feature = "test_input")] + let file_contents = fs::read_to_string("tests/day6.txt")?; + let lines = file_contents.lines(); let mut line_counter = 0; diff --git a/2022/src/days/day7.rs b/2022/src/days/day7.rs index 744b084..dc73fc7 100644 --- a/2022/src/days/day7.rs +++ b/2022/src/days/day7.rs @@ -86,8 +86,11 @@ fn part_two_recurse<'a>(map: &mut BTreeMap, dir: &'a Dir) { } pub fn run() -> Result<()> { + #[cfg(feature = "test_input")] + let file_contents = fs::read_to_string("tests/day7.txt")?; + + #[cfg(not(feature = "test_input"))] let file_contents = fs::read_to_string("day7.txt")?; - //let file_contents = fs::read_to_string("tests/day7.txt")?; let mut root = Dir { name: String::from("/"), diff --git a/2022/tests/day1.txt b/2022/tests/day1.txt new file mode 100644 index 0000000..2094f91 --- /dev/null +++ b/2022/tests/day1.txt @@ -0,0 +1,14 @@ +1000 +2000 +3000 + +4000 + +5000 +6000 + +7000 +8000 +9000 + +10000