From 8eb3472718cbe1e14fbdea1a4627790669a5bdb2 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Thu, 8 Dec 2022 00:04:06 +0100 Subject: [PATCH] Clean up: factorize input test --- src/day01.rs | 46 ++++++++++++++++------------------------------ src/day02.rs | 17 +++++++---------- src/day03.rs | 18 +++++------------- src/day04.rs | 17 +++++------------ src/day05.rs | 20 ++++++-------------- src/day07.rs | 43 +++++++++---------------------------------- src/main.rs | 2 +- 7 files changed, 49 insertions(+), 114 deletions(-) diff --git a/src/day01.rs b/src/day01.rs index 0a397c0..79d7f73 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -31,44 +31,30 @@ pub fn get_sum_most_three_calories(calories: &[i64]) -> i64 { mod tests { use super::*; - #[test] - fn part1() { - let calories = - "1000 - 2000 - 3000 + static CALORIES: &str = + "1000 + 2000 + 3000 + + 4000 - 4000 + 5000 + 6000 - 5000 - 6000 + 7000 + 8000 + 9000 - 7000 - 8000 - 9000 + 10000"; - 10000"; - assert_eq!(get_most_calories(&read_calories(calories.as_bytes())), 24000); + #[test] + fn part1() { + assert_eq!(get_most_calories(&read_calories(CALORIES.as_bytes())), 24000); } #[test] fn part2() { - let calories = - "1000 - 2000 - 3000 - - 4000 - - 5000 - 6000 - - 7000 - 8000 - 9000 - - 10000"; - assert_eq!(get_sum_most_three_calories(&read_calories(calories.as_bytes())), 45000); + assert_eq!(get_sum_most_three_calories(&read_calories(CALORIES.as_bytes())), 45000); } } \ No newline at end of file diff --git a/src/day02.rs b/src/day02.rs index 1094862..2335520 100644 --- a/src/day02.rs +++ b/src/day02.rs @@ -69,21 +69,18 @@ pub fn get_score(shapes: &[(Shape, Shape)]) -> i32 { mod tests { use super::*; + static STRATEGY_GUIDE: &str = + "A Y + B X + C Z"; + #[test] fn part1() { - let stategy_guide = - "A Y - B X - C Z"; - assert_eq!(get_score(&read_shapes(stategy_guide.as_bytes())), 15); + assert_eq!(get_score(&read_shapes(STRATEGY_GUIDE.as_bytes())), 15); } #[test] fn part2() { - let stategy_guide = - "A Y - B X - C Z"; - assert_eq!(get_score(&read_shapes_2(stategy_guide.as_bytes())), 12); + assert_eq!(get_score(&read_shapes_2(STRATEGY_GUIDE.as_bytes())), 12); } } \ No newline at end of file diff --git a/src/day03.rs b/src/day03.rs index c798497..332f1a1 100644 --- a/src/day03.rs +++ b/src/day03.rs @@ -59,9 +59,7 @@ pub fn badge_sum(rucksacks: &[Vec]) -> i32 { mod tests { use super::*; - #[test] - fn part1() { - let rucksacks = + static RUCKSACKS: &str = "vJrwpWtwJgWrhcsFMMfFFhFp jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL PmmdzqPrVvPwwTWBwg @@ -69,19 +67,13 @@ wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn ttgJtRGJQctTZtZT CrZsJsPPZsGzwwsLwLmpwMDw"; - assert_eq!(priority_sum(&parse(rucksacks)), 157); + #[test] + fn part1() { + assert_eq!(priority_sum(&parse(RUCKSACKS)), 157); } #[test] fn part2() { - let rucksacks = - "vJrwpWtwJgWrhcsFMMfFFhFp -jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL -PmmdzqPrVvPwwTWBwg -wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn -ttgJtRGJQctTZtZT -CrZsJsPPZsGzwwsLwLmpwMDw"; - - assert_eq!(badge_sum(&parse(rucksacks)), 70); + assert_eq!(badge_sum(&parse(RUCKSACKS)), 70); } } \ No newline at end of file diff --git a/src/day04.rs b/src/day04.rs index 42302fd..e4dda72 100644 --- a/src/day04.rs +++ b/src/day04.rs @@ -28,27 +28,20 @@ pub fn number_overlaps(pairs: &[Pair]) -> i32 { mod tests { use super::*; - #[test] - fn part1() { - let pairs ="2-4,6-8 + static PAIRS: &str ="2-4,6-8 2-3,4-5 5-7,7-9 2-8,3-7 6-6,4-6 2-6,4-8"; - assert_eq!(number_fully_contain(&parse(pairs)), 2); + #[test] + fn part1() { + assert_eq!(number_fully_contain(&parse(PAIRS)), 2); } #[test] fn part2() { - let pairs ="2-4,6-8 -2-3,4-5 -5-7,7-9 -2-8,3-7 -6-6,4-6 -2-6,4-8"; - - assert_eq!(number_overlaps(&parse(pairs)), 4); + assert_eq!(number_overlaps(&parse(PAIRS)), 4); } } \ No newline at end of file diff --git a/src/day05.rs b/src/day05.rs index e727784..c04bde7 100644 --- a/src/day05.rs +++ b/src/day05.rs @@ -72,9 +72,7 @@ pub fn get_top_as_string(stacks: &Stacks) -> String { mod tests { use super::*; - #[test] - fn part1() { - let stacks_and_moves =" [D] + static STACKS_AND_MOVES: &str =" [D] [N] [C] [Z] [M] [P] 1 2 3 @@ -83,23 +81,17 @@ move 1 from 2 to 1 move 3 from 1 to 3 move 2 from 2 to 1 move 1 from 1 to 2"; - let (mut stacks, moves) = parse(stacks_and_moves); + + #[test] + fn part1() { + let (mut stacks, moves) = parse(STACKS_AND_MOVES); apply_moves_by_crate_mover_9000(&mut stacks, &moves); assert_eq!(get_top_as_string(&stacks), "CMZ"); } #[test] fn part2() { - let stacks_and_moves =" [D] -[N] [C] -[Z] [M] [P] - 1 2 3 - -move 1 from 2 to 1 -move 3 from 1 to 3 -move 2 from 2 to 1 -move 1 from 1 to 2"; - let (mut stacks, moves) = parse(stacks_and_moves); + let (mut stacks, moves) = parse(STACKS_AND_MOVES); apply_moves_by_crate_mover_9001(&mut stacks, &moves); assert_eq!(get_top_as_string(&stacks), "MCD"); } diff --git a/src/day07.rs b/src/day07.rs index c3f5fb1..cc146b4 100644 --- a/src/day07.rs +++ b/src/day07.rs @@ -56,9 +56,7 @@ pub fn parse(input: &str) -> Dir { mod tests { use super::*; - #[test] - fn part1() { - let commands = "$ cd / + static INPUT: &str = "$ cd / $ ls dir a 14848514 b.txt @@ -81,7 +79,10 @@ $ ls 8033020 d.log 5626152 d.ext 7214296 k"; - let root = parse(commands); + + #[test] + fn part1() { + let root = parse(INPUT); let mut sizes: Vec = Vec::new(); root.dir_sizes(|size| size <= 100_000, &mut sizes); assert_eq!(sizes.iter().sum::(), 95_437); @@ -89,37 +90,11 @@ $ ls #[test] fn part2() { - let commands = "$ cd / -$ ls -dir a -14848514 b.txt -8504156 c.dat -dir d -$ cd a -$ ls -dir e -29116 f -2557 g -62596 h.lst -$ cd e -$ ls -584 i -$ cd .. -$ cd .. -$ cd d -$ ls -4060174 j -8033020 d.log -5626152 d.ext -7214296 k"; - let root = parse(commands); - + let root = parse(INPUT); let root_size = root.dir_sizes(|size| size <= 100_000, &mut Vec::new()); - - let required = root_size - (70_000_000 - 30_000_000); + let to_free = root_size - (70_000_000 - 30_000_000); let mut sizes: Vec = Vec::new(); - root.dir_sizes(|size| size >= required, &mut sizes); - - assert_eq!(*sizes.iter().min().unwrap(), 24933642); + root.dir_sizes(|size| size >= to_free, &mut sizes); + assert_eq!(*sizes.iter().min().unwrap(), 2_493_3642); } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 91e1c7a..cf08101 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,7 @@ fn day06() -> String { fn day07() -> String { let root = day07::parse(&fs::read_to_string("data/day07.input").unwrap()); - let (root_size, sum_part1 ) = { + let (root_size, sum_part1, ) = { let mut sizes: Vec = Vec::new(); (root.dir_sizes(|size| size <= 100_000, &mut sizes), sizes.iter().sum::()) }; -- 2.45.2