Allow for different return types
This commit is contained in:
parent
f290ea91bc
commit
119d172891
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use aoc::Solver;
|
use aoc::{Solver, Output};
|
||||||
|
|
||||||
// -- Runners --
|
// -- Runners --
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
@ -12,19 +12,19 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn part1_test1() -> Result<()> {
|
fn part1_test1() -> Result<()> {
|
||||||
Day::test(aoc::Part::ONE, "test-1", 24000)
|
Day::test(aoc::Part::ONE, "test-1", Output::Number(24000))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_test1() -> Result<()> {
|
fn part2_test1() -> Result<()> {
|
||||||
Day::test(aoc::Part::TWO, "test-1", 45000)
|
Day::test(aoc::Part::TWO, "test-1", Output::Number(45000))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part1_solution() -> Result<()> {
|
fn part1_solution() -> Result<()> {
|
||||||
Day::test(aoc::Part::ONE, "input", 70116)
|
Day::test(aoc::Part::ONE, "input", Output::Number(70116))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_solution() -> Result<()> {
|
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
|
1
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(input: &str) -> u32 {
|
fn part1(input: &str) -> Output {
|
||||||
input.split("\n\n")
|
let result = input.split("\n\n")
|
||||||
.map(|elf| elf.lines()
|
.map(|elf| elf.lines()
|
||||||
.flat_map(|snack| snack.parse::<u32>())
|
.flat_map(|snack| snack.parse::<u32>())
|
||||||
.sum())
|
.sum())
|
||||||
.max()
|
.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")
|
let mut elfs: Vec<u32> = input.split("\n\n")
|
||||||
.map(|elf| elf.lines()
|
.map(|elf| elf.lines()
|
||||||
.flat_map(|snack| snack.parse::<u32>())
|
.flat_map(|snack| snack.parse::<u32>())
|
||||||
|
@ -53,6 +55,8 @@ impl aoc::Solver for Day {
|
||||||
|
|
||||||
elfs.sort_by(|a, b| b.cmp(a));
|
elfs.sort_by(|a, b| b.cmp(a));
|
||||||
|
|
||||||
elfs.iter().take(3).sum()
|
let result = elfs.iter().take(3).sum();
|
||||||
|
|
||||||
|
Output::Number(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use aoc::Solver;
|
use aoc::{Solver, Output};
|
||||||
|
|
||||||
// -- Runners --
|
// -- Runners --
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
@ -12,19 +12,19 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn part1_test1() -> Result<()> {
|
fn part1_test1() -> Result<()> {
|
||||||
Day::test(aoc::Part::ONE, "test-1", 15)
|
Day::test(aoc::Part::ONE, "test-1", Output::Number(15))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_test1() -> Result<()> {
|
fn part2_test1() -> Result<()> {
|
||||||
Day::test(aoc::Part::TWO, "test-1", 12)
|
Day::test(aoc::Part::TWO, "test-1", Output::Number(12))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part1_solution() -> Result<()> {
|
fn part1_solution() -> Result<()> {
|
||||||
Day::test(aoc::Part::ONE, "input", 14264)
|
Day::test(aoc::Part::ONE, "input", Output::Number(14264))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_solution() -> Result<()> {
|
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
|
2
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(input: &str) -> u32 {
|
fn part1(input: &str) -> Output {
|
||||||
input.lines()
|
let result = input.lines()
|
||||||
.filter_map(|round| round.split_once(" "))
|
.filter_map(|round| round.split_once(" "))
|
||||||
.map(|(a, b)| (Hand::from(a), Hand::from(b)))
|
.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 {
|
fn part2(input: &str) -> Output {
|
||||||
input.lines()
|
let result = input.lines()
|
||||||
.filter_map(|round| round.split_once(" "))
|
.filter_map(|round| round.split_once(" "))
|
||||||
.map(|(a, b)| {
|
.map(|(a, b)| {
|
||||||
let opponent = Hand::from(a);
|
let opponent = Hand::from(a);
|
||||||
(opponent, opponent.strategy(b))
|
(opponent, opponent.strategy(b))
|
||||||
})
|
})
|
||||||
.fold(0, calc_score)
|
.fold(0, calc_score);
|
||||||
|
|
||||||
|
Output::Number(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use aoc::Solver;
|
use aoc::{Solver, Output};
|
||||||
|
|
||||||
// -- Runners --
|
// -- Runners --
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
@ -12,19 +12,19 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn part1_test1() -> Result<()> {
|
fn part1_test1() -> Result<()> {
|
||||||
Day::test(aoc::Part::ONE, "test-1", 157)
|
Day::test(aoc::Part::ONE, "test-1", Output::Number(157))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_test1() -> Result<()> {
|
fn part2_test1() -> Result<()> {
|
||||||
Day::test(aoc::Part::TWO, "test-1", 70)
|
Day::test(aoc::Part::TWO, "test-1", Output::Number(70))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part1_solution() -> Result<()> {
|
fn part1_solution() -> Result<()> {
|
||||||
Day::test(aoc::Part::ONE, "input", 8298)
|
Day::test(aoc::Part::ONE, "input", Output::Number(8298))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_solution() -> Result<()> {
|
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
|
3
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(input: &str) -> u32 {
|
fn part1(input: &str) -> Output {
|
||||||
input.lines()
|
let result = input.lines()
|
||||||
.map(|line| line.split_at(line.len()/2))
|
.map(|line| line.split_at(line.len()/2))
|
||||||
.map(|(a, b)| {
|
.map(|(a, b)| {
|
||||||
for c in a.chars() {
|
for c in a.chars() {
|
||||||
|
@ -73,15 +73,19 @@ impl aoc::Solver for Day {
|
||||||
unreachable!("No characters in common, this should never happen")
|
unreachable!("No characters in common, this should never happen")
|
||||||
})
|
})
|
||||||
.map(convert)
|
.map(convert)
|
||||||
.sum()
|
.sum();
|
||||||
|
|
||||||
|
Output::Number(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(input: &str) -> u32 {
|
fn part2(input: &str) -> Output {
|
||||||
input.lines()
|
let result = input.lines()
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.chunks(3)
|
.chunks(3)
|
||||||
.map(find_common)
|
.map(find_common)
|
||||||
.map(convert)
|
.map(convert)
|
||||||
.sum()
|
.sum();
|
||||||
|
|
||||||
|
Output::Number(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use aoc::Solver;
|
use aoc::{Solver, Output};
|
||||||
|
|
||||||
// -- Runners --
|
// -- Runners --
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
@ -13,19 +13,19 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn part1_test1() -> Result<()> {
|
fn part1_test1() -> Result<()> {
|
||||||
Day::test(aoc::Part::ONE, "test-1", 2)
|
Day::test(aoc::Part::ONE, "test-1", Output::Number(2))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_test1() -> Result<()> {
|
fn part2_test1() -> Result<()> {
|
||||||
Day::test(aoc::Part::TWO, "test-1", 4)
|
Day::test(aoc::Part::TWO, "test-1", Output::Number(4))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part1_solution() -> Result<()> {
|
fn part1_solution() -> Result<()> {
|
||||||
Day::test(aoc::Part::ONE, "input", 567)
|
Day::test(aoc::Part::ONE, "input", Output::Number(567))
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_solution() -> Result<()> {
|
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
|
4
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(input: &str) -> u32 {
|
fn part1(input: &str) -> Output {
|
||||||
input
|
let result = input
|
||||||
.lines()
|
.lines()
|
||||||
.map(transform)
|
.map(transform)
|
||||||
.filter(contains)
|
.filter(contains)
|
||||||
.count() as u32
|
.count() as u32;
|
||||||
|
|
||||||
|
Output::Number(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(input: &str) -> u32 {
|
fn part2(input: &str) -> Output {
|
||||||
input
|
let result = input
|
||||||
.lines()
|
.lines()
|
||||||
.map(transform)
|
.map(transform)
|
||||||
.filter(overlaps)
|
.filter(overlaps)
|
||||||
.count() as u32
|
.count() as u32;
|
||||||
|
|
||||||
|
Output::Number(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use core::fmt;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
@ -7,12 +8,30 @@ pub enum Part {
|
||||||
TWO
|
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 {
|
pub trait Solver {
|
||||||
fn day() -> u8;
|
fn day() -> u8;
|
||||||
fn part1(input: &str) -> u32;
|
fn part1(input: &str) -> Output;
|
||||||
fn part2(input: &str) -> u32;
|
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
|
// Select the right function
|
||||||
let fun = match part {
|
let fun = match part {
|
||||||
Part::ONE => Self::part1,
|
Part::ONE => Self::part1,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use aoc::Solver;
|
use aoc::{Solver, Output};
|
||||||
|
|
||||||
// -- Runners --
|
// -- Runners --
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
@ -12,7 +12,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn part1_test1() -> Result<()> {
|
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")
|
todo!("Day not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(input: &str) -> u32 {
|
fn part1(input: &str) -> Output {
|
||||||
0
|
Output::Number(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(input: &str) -> u32 {
|
fn part2(input: &str) -> Output {
|
||||||
0
|
Output::Number(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user