Allow for different return types

This commit is contained in:
Dreaded_X 2022-12-05 21:45:26 +01:00
parent f290ea91bc
commit 119d172891
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
6 changed files with 87 additions and 52 deletions

View File

@ -1,5 +1,5 @@
use anyhow::Result;
use aoc::Solver;
use aoc::{Solver, Output};
// -- Runners --
fn main() -> Result<()> {
@ -12,19 +12,19 @@ mod tests {
#[test]
fn part1_test1() -> Result<()> {
Day::test(aoc::Part::ONE, "test-1", 24000)
Day::test(aoc::Part::ONE, "test-1", Output::Number(24000))
}
#[test]
fn part2_test1() -> Result<()> {
Day::test(aoc::Part::TWO, "test-1", 45000)
Day::test(aoc::Part::TWO, "test-1", Output::Number(45000))
}
#[test]
fn part1_solution() -> Result<()> {
Day::test(aoc::Part::ONE, "input", 70116)
Day::test(aoc::Part::ONE, "input", Output::Number(70116))
}
#[test]
fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 206582)
Day::test(aoc::Part::TWO, "input", Output::Number(206582))
}
}
@ -35,16 +35,18 @@ impl aoc::Solver for Day {
1
}
fn part1(input: &str) -> u32 {
input.split("\n\n")
fn part1(input: &str) -> Output {
let result = input.split("\n\n")
.map(|elf| elf.lines()
.flat_map(|snack| snack.parse::<u32>())
.sum())
.max()
.unwrap()
.unwrap();
Output::Number(result)
}
fn part2(input: &str) -> u32 {
fn part2(input: &str) -> Output {
let mut elfs: Vec<u32> = input.split("\n\n")
.map(|elf| elf.lines()
.flat_map(|snack| snack.parse::<u32>())
@ -53,6 +55,8 @@ impl aoc::Solver for Day {
elfs.sort_by(|a, b| b.cmp(a));
elfs.iter().take(3).sum()
let result = elfs.iter().take(3).sum();
Output::Number(result)
}
}

View File

@ -1,5 +1,5 @@
use anyhow::Result;
use aoc::Solver;
use aoc::{Solver, Output};
// -- Runners --
fn main() -> Result<()> {
@ -12,19 +12,19 @@ mod tests {
#[test]
fn part1_test1() -> Result<()> {
Day::test(aoc::Part::ONE, "test-1", 15)
Day::test(aoc::Part::ONE, "test-1", Output::Number(15))
}
#[test]
fn part2_test1() -> Result<()> {
Day::test(aoc::Part::TWO, "test-1", 12)
Day::test(aoc::Part::TWO, "test-1", Output::Number(12))
}
#[test]
fn part1_solution() -> Result<()> {
Day::test(aoc::Part::ONE, "input", 14264)
Day::test(aoc::Part::ONE, "input", Output::Number(14264))
}
#[test]
fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 12382)
Day::test(aoc::Part::TWO, "input", Output::Number(12382))
}
}
@ -106,20 +106,24 @@ impl aoc::Solver for Day {
2
}
fn part1(input: &str) -> u32 {
input.lines()
fn part1(input: &str) -> Output {
let result = input.lines()
.filter_map(|round| round.split_once(" "))
.map(|(a, b)| (Hand::from(a), Hand::from(b)))
.fold(0, calc_score)
.fold(0, calc_score);
Output::Number(result)
}
fn part2(input: &str) -> u32 {
input.lines()
fn part2(input: &str) -> Output {
let result = input.lines()
.filter_map(|round| round.split_once(" "))
.map(|(a, b)| {
let opponent = Hand::from(a);
(opponent, opponent.strategy(b))
})
.fold(0, calc_score)
.fold(0, calc_score);
Output::Number(result)
}
}

View File

@ -1,5 +1,5 @@
use anyhow::Result;
use aoc::Solver;
use aoc::{Solver, Output};
// -- Runners --
fn main() -> Result<()> {
@ -12,19 +12,19 @@ mod tests {
#[test]
fn part1_test1() -> Result<()> {
Day::test(aoc::Part::ONE, "test-1", 157)
Day::test(aoc::Part::ONE, "test-1", Output::Number(157))
}
#[test]
fn part2_test1() -> Result<()> {
Day::test(aoc::Part::TWO, "test-1", 70)
Day::test(aoc::Part::TWO, "test-1", Output::Number(70))
}
#[test]
fn part1_solution() -> Result<()> {
Day::test(aoc::Part::ONE, "input", 8298)
Day::test(aoc::Part::ONE, "input", Output::Number(8298))
}
#[test]
fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 2708)
Day::test(aoc::Part::TWO, "input", Output::Number(2708))
}
}
@ -60,8 +60,8 @@ impl aoc::Solver for Day {
3
}
fn part1(input: &str) -> u32 {
input.lines()
fn part1(input: &str) -> Output {
let result = input.lines()
.map(|line| line.split_at(line.len()/2))
.map(|(a, b)| {
for c in a.chars() {
@ -73,15 +73,19 @@ impl aoc::Solver for Day {
unreachable!("No characters in common, this should never happen")
})
.map(convert)
.sum()
.sum();
Output::Number(result)
}
fn part2(input: &str) -> u32 {
input.lines()
fn part2(input: &str) -> Output {
let result = input.lines()
.collect::<Vec<_>>()
.chunks(3)
.map(find_common)
.map(convert)
.sum()
.sum();
Output::Number(result)
}
}

View File

@ -1,6 +1,6 @@
use std::cmp;
use anyhow::Result;
use aoc::Solver;
use aoc::{Solver, Output};
// -- Runners --
fn main() -> Result<()> {
@ -13,19 +13,19 @@ mod tests {
#[test]
fn part1_test1() -> Result<()> {
Day::test(aoc::Part::ONE, "test-1", 2)
Day::test(aoc::Part::ONE, "test-1", Output::Number(2))
}
#[test]
fn part2_test1() -> Result<()> {
Day::test(aoc::Part::TWO, "test-1", 4)
Day::test(aoc::Part::TWO, "test-1", Output::Number(4))
}
#[test]
fn part1_solution() -> Result<()> {
Day::test(aoc::Part::ONE, "input", 567)
Day::test(aoc::Part::ONE, "input", Output::Number(567))
}
#[test]
fn part2_solution() -> Result<()> {
Day::test(aoc::Part::TWO, "input", 907)
Day::test(aoc::Part::TWO, "input", Output::Number(907))
}
}
@ -76,19 +76,23 @@ impl aoc::Solver for Day {
4
}
fn part1(input: &str) -> u32 {
input
fn part1(input: &str) -> Output {
let result = input
.lines()
.map(transform)
.filter(contains)
.count() as u32
.count() as u32;
Output::Number(result)
}
fn part2(input: &str) -> u32 {
input
fn part2(input: &str) -> Output {
let result = input
.lines()
.map(transform)
.filter(overlaps)
.count() as u32
.count() as u32;
Output::Number(result)
}
}

View File

@ -1,3 +1,4 @@
use core::fmt;
use std::fs;
use anyhow::{Context, Result};
@ -7,12 +8,30 @@ pub enum Part {
TWO
}
#[derive(Debug,PartialEq, PartialOrd)]
pub enum Output {
Number(u32),
String(String),
}
impl fmt::Display for Output {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Output::Number(i) => write!(f, "{}", i)?,
Output::String(s) => write!(f, "{}", s)?,
_ => panic!("fmt::Display not implemented")
}
Ok(())
}
}
pub trait Solver {
fn day() -> u8;
fn part1(input: &str) -> u32;
fn part2(input: &str) -> u32;
fn part1(input: &str) -> Output;
fn part2(input: &str) -> Output;
fn test(part: Part, name: &str, result: u32) -> Result<()> {
fn test(part: Part, name: &str, result: Output) -> Result<()> {
// Select the right function
let fun = match part {
Part::ONE => Self::part1,

View File

@ -1,5 +1,5 @@
use anyhow::Result;
use aoc::Solver;
use aoc::{Solver, Output};
// -- Runners --
fn main() -> Result<()> {
@ -12,7 +12,7 @@ mod tests {
#[test]
fn part1_test1() -> Result<()> {
Day::test(aoc::Part::ONE, "test-1", 0)
Day::test(aoc::Part::ONE, "test-1", Output::Number(0))
}
}
@ -23,11 +23,11 @@ impl aoc::Solver for Day {
todo!("Day not set")
}
fn part1(input: &str) -> u32 {
0
fn part1(input: &str) -> Output {
Output::Number(0)
}
fn part2(input: &str) -> u32 {
0
fn part2(input: &str) -> Output {
Output::Number(0)
}
}