From cef5587b56dada93897a82d6374a64bd4e7ed749 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Sun, 4 Dec 2022 11:50:47 +0100 Subject: [PATCH] Add tests for day 1-3 Also handle different line endings --- rust/src/day01/main.rs | 6 ++++-- rust/src/day01/part_1.rs | 12 +++++++++--- rust/src/day01/part_2.rs | 10 ++++++++-- rust/src/day02/part_1.rs | 8 +++++++- rust/src/day02/part_2.rs | 8 +++++++- rust/src/day03/part_1.rs | 8 +++++++- rust/src/day03/part_2.rs | 8 +++++++- 7 files changed, 49 insertions(+), 11 deletions(-) diff --git a/rust/src/day01/main.rs b/rust/src/day01/main.rs index cc219d2..ec6d538 100644 --- a/rust/src/day01/main.rs +++ b/rust/src/day01/main.rs @@ -6,6 +6,8 @@ mod part_2; use part_2::part_2; pub fn main() { - part_1(INPUT); - part_2(INPUT); + let input = INPUT.replace("\r\n", "\n"); + + part_1(&input); + part_2(&input); } diff --git a/rust/src/day01/part_1.rs b/rust/src/day01/part_1.rs index dc92bf7..1a482d0 100644 --- a/rust/src/day01/part_1.rs +++ b/rust/src/day01/part_1.rs @@ -1,9 +1,9 @@ -pub(crate) fn part_1(input: &'static str) { +pub(crate) fn part_1(input: &str) -> u32 { let maximum = input .split("\n\n") .map(|inventory| { inventory - .split('\n') + .lines() .map(|item| { item.parse::() .expect("Input isn't clean. Non-number found") @@ -13,5 +13,11 @@ pub(crate) fn part_1(input: &'static str) { .max() .expect("No highest inventory found. Input unclean?"); - println!("Part 1: {maximum}") + println!("Part 1: {maximum}"); + maximum +} + +#[test] +fn test_with_solution() { + assert_eq!(part_1(&crate::INPUT.replace("\r\n", "\n")), 69528); } diff --git a/rust/src/day01/part_2.rs b/rust/src/day01/part_2.rs index 4b6af6e..c237b58 100644 --- a/rust/src/day01/part_2.rs +++ b/rust/src/day01/part_2.rs @@ -1,9 +1,9 @@ -pub(crate) fn part_2(input: &'static str) { +pub(crate) fn part_2(input: &str) -> u32 { let mut calories = input .split("\n\n") .map(|inventory| { inventory - .split('\n') + .lines() .map(|item| { item.parse::() .expect("Input isn't clean. Non-number found") @@ -16,4 +16,10 @@ pub(crate) fn part_2(input: &'static str) { let top_three = calories.iter().take(3).sum::(); println!("Part 2: {top_three}"); + top_three +} + +#[test] +fn test_with_solution() { + assert_eq!(part_2(&crate::INPUT.replace("\r\n", "\n")), 206152); } diff --git a/rust/src/day02/part_1.rs b/rust/src/day02/part_1.rs index 8ab22f0..424bd17 100644 --- a/rust/src/day02/part_1.rs +++ b/rust/src/day02/part_1.rs @@ -1,8 +1,14 @@ use crate::Shape; -pub(crate) fn part_1(input: &[[Shape; 2]]) { +pub(crate) fn part_1(input: &[[Shape; 2]]) -> u64 { let final_score = input.iter().fold(0u64, |acc, [enemy, mine]| { acc + u64::from(&mine.play_against(enemy)) + u64::from(mine) }); println!("Part 1: {final_score}"); + final_score +} + +#[test] +fn test_with_solution() { + assert_eq!(part_1(&crate::parse_input_part_1()), 13221); } diff --git a/rust/src/day02/part_2.rs b/rust/src/day02/part_2.rs index cac7646..12f4707 100644 --- a/rust/src/day02/part_2.rs +++ b/rust/src/day02/part_2.rs @@ -1,6 +1,6 @@ use crate::{GameResult, Shape}; -pub(crate) fn part_2(input: &[(Shape, GameResult)]) { +pub(crate) fn part_2(input: &[(Shape, GameResult)]) -> u64 { let final_score = input .iter() .fold(0u64, |acc, (enemy_play, desired_result)| { @@ -8,4 +8,10 @@ pub(crate) fn part_2(input: &[(Shape, GameResult)]) { + u64::from(desired_result) }); println!("Part 2: {final_score}"); + final_score +} + +#[test] +fn test_with_solution() { + assert_eq!(part_2(&crate::parse_input_part_2()), 13131); } diff --git a/rust/src/day03/part_1.rs b/rust/src/day03/part_1.rs index 14f8fd0..d185460 100644 --- a/rust/src/day03/part_1.rs +++ b/rust/src/day03/part_1.rs @@ -1,6 +1,6 @@ use std::collections::HashSet; -pub(crate) fn part_1(input: &'static str) { +pub(crate) fn part_1(input: &'static str) -> u64 { let rucksacks = input.lines(); let compartments = rucksacks.map(|rucksack| { let compartment_size = rucksack.len() / 2; @@ -33,4 +33,10 @@ pub(crate) fn part_1(input: &'static str) { running_sum += crate::character_priority(*same_bytes[0]); } println!("Part 1: {running_sum}"); + running_sum +} + +#[test] +fn test_with_solution() { + assert_eq!(part_1(crate::INPUT), 7742); } diff --git a/rust/src/day03/part_2.rs b/rust/src/day03/part_2.rs index ecbf423..7f67866 100644 --- a/rust/src/day03/part_2.rs +++ b/rust/src/day03/part_2.rs @@ -2,7 +2,7 @@ use std::collections::HashSet; use crate::character_priority; -pub(crate) fn part_2(input: &'static str) { +pub(crate) fn part_2(input: &'static str) -> u64 { let rucksacks = input.lines(); let compartments = rucksacks .map(|rucksack| rucksack.bytes().collect::>()) @@ -20,4 +20,10 @@ pub(crate) fn part_2(input: &'static str) { running_sum += character_priority(group_bytes[0]); } println!("Part 2: {running_sum}"); + running_sum +} + +#[test] +fn test_with_solution() { + assert_eq!(part_2(crate::INPUT), 2276); }