Add tests for day 1-3

Also handle different line endings
This commit is contained in:
Tobias Berger 2022-12-04 11:50:47 +01:00
parent 80e9066b1d
commit cef5587b56
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
7 changed files with 49 additions and 11 deletions

View file

@ -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);
}

View file

@ -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::<u32>()
.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);
}

View file

@ -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::<u32>()
.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::<u32>();
println!("Part 2: {top_three}");
top_three
}
#[test]
fn test_with_solution() {
assert_eq!(part_2(&crate::INPUT.replace("\r\n", "\n")), 206152);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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::<HashSet<_>>())
@ -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);
}