aoc: stop printing inside run functions

This will make benching implementations easier
This commit is contained in:
Antoine Martin 2019-12-06 19:06:49 +01:00
parent 6f06d5d7ca
commit 4bff689da0
21 changed files with 139 additions and 75 deletions

View file

@ -7,7 +7,7 @@ macro_rules! err {
($($string:expr),+) => (Box::<dyn std::error::Error>::from(format!($($string),+)))
}
type DayFunc = fn() -> Result<()>;
pub type DayFunc = fn() -> Result<String>;
pub fn run(days: &[DayFunc]) -> Result<()> {
let mut args = env::args();
@ -16,14 +16,16 @@ pub fn run(days: &[DayFunc]) -> Result<()> {
match args.next() {
Some(arg) => {
let day: usize = arg.parse().expect("Please provide a day number");
days[day - 1]().unwrap_or_else(|e| eprintln!("error running day specified: {}", e));
let res =
days[day - 1]().or_else(|e| Err(err!("error running day specified: {}", e)))?;
println!("{}", res);
}
None => {
for (i, day) in days.iter().enumerate() {
let i = i + 1;
println!("day{}: ", i);
day().unwrap_or_else(|e| eprintln!("error running day {}: {}", i, e));
println!();
let res = day().or_else(|e| Err(err!("error running day {}: {}", i, e)))?;
println!("{}", res);
}
}
}

View file

@ -1,13 +1,17 @@
use std::fmt::Write;
use aoc::err;
use aoc::Result;
const INPUT: &str = include_str!("../input/day01.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<i64> {

View file

@ -1,4 +1,5 @@
use std::error::Error;
use std::fmt::Write;
use std::str::FromStr;
use aoc::err;
@ -6,16 +7,18 @@ use aoc::Result;
const INPUT: &str = include_str!("../input/day02.txt");
pub fn run() -> Result<()> {
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
let presents: Vec<Present> = INPUT
.lines()
.map(|line| line.parse())
.collect::<Result<_>>()?;
println!("part 1: {}", part1(&presents));
println!("part 2: {}", part2(&presents));
writeln!(res, "part 1: {}", part1(&presents))?;
writeln!(res, "part 2: {}", part2(&presents))?;
Ok(())
Ok(res)
}
fn wrapping_paper(present: &Present) -> u64 {

View file

@ -1,15 +1,18 @@
use std::collections::HashSet;
use std::fmt::Write;
use aoc::err;
use aoc::Result;
const INPUT: &str = include_str!("../input/day03.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<usize> {

View file

@ -1,3 +1,5 @@
use std::fmt::Write;
use md5::{Digest, Md5};
use aoc::err;
@ -5,11 +7,13 @@ use aoc::Result;
const INPUT: &str = include_str!("../input/day04.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<u64> {

View file

@ -1,12 +1,16 @@
use std::fmt::Write;
use aoc::Result;
const INPUT: &str = include_str!("../input/day05.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT));
println!("part 2: {}", part2(INPUT));
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT))?;
writeln!(res, "part 2: {}", part2(INPUT))?;
Ok(res)
}
fn part1(input: &str) -> usize {

View file

@ -1,4 +1,5 @@
use std::error::Error;
use std::fmt::Write;
use std::str::FromStr;
use aoc::err;
@ -6,11 +7,13 @@ use aoc::Result;
const INPUT: &str = include_str!("../input/day06.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<usize> {

View file

@ -1,3 +1,4 @@
use aoc::DayFunc;
use aoc::Result;
use aoc2015::day01;
@ -8,7 +9,7 @@ use aoc2015::day05;
use aoc2015::day06;
fn main() -> Result<()> {
let days: &[fn() -> Result<()>] = &[
let days: &[DayFunc] = &[
day01::run,
day02::run,
day03::run,

View file

@ -1,14 +1,17 @@
use std::collections::HashSet;
use std::fmt::Write;
use aoc::Result;
const INPUT: &str = include_str!("../input/day01.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<i32> {

View file

@ -1,14 +1,17 @@
use std::collections::HashMap;
use std::fmt::Write;
use aoc::Result;
const INPUT: &str = include_str!("../input/day02.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<u32> {

View file

@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::error::Error;
use std::fmt::Write;
use std::str::FromStr;
use aoc::err;
@ -8,11 +9,13 @@ use aoc::Result;
const INPUT: &str = include_str!("../input/day03.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
struct Claim {

View file

@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::error::Error;
use std::fmt::Write;
use std::str::FromStr;
use aoc::err;
@ -13,10 +14,12 @@ fn sorted_lines(input: &str) -> String {
lines.join("\n")
}
pub fn run() -> Result<()> {
println!("part 1: {}", part1(&sorted_lines(INPUT))?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(&sorted_lines(INPUT))?)?;
Ok(res)
}
#[derive(Debug)]

View file

@ -1,14 +1,17 @@
use std::collections::HashSet;
use std::fmt::Write;
use aoc::Result;
const INPUT: &str = include_str!("../input/day05.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT));
println!("part 2: {}", part2(INPUT));
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT))?;
writeln!(res, "part 2: {}", part2(INPUT))?;
Ok(res)
}
fn same_type(a: char, b: char) -> bool {

View file

@ -1,3 +1,4 @@
use aoc::DayFunc;
use aoc::Result;
use aoc2018::day01;
@ -7,7 +8,7 @@ use aoc2018::day04;
use aoc2018::day05;
fn main() -> Result<()> {
let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run, day04::run, day05::run];
let days: &[DayFunc] = &[day01::run, day02::run, day03::run, day04::run, day05::run];
aoc::run(days)
}

View file

@ -1,12 +1,16 @@
use std::fmt::Write;
use aoc::Result;
const INPUT: &str = include_str!("../input/day01.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn fuel_needed(module_weight: u64) -> u64 {

View file

@ -1,3 +1,5 @@
use std::fmt::Write;
use aoc::err;
use aoc::Result;
@ -12,12 +14,15 @@ fn parse_intcode(input: &str) -> Result<Vec<usize>> {
.collect()
}
pub fn run() -> Result<()> {
let intcode = parse_intcode(INPUT)?;
println!("part 1: {}", part1(&mut intcode.clone())?);
println!("part 2: {}", part2(&intcode, PART2_EXPECTED)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
let intcode = parse_intcode(INPUT)?;
writeln!(res, "part 1: {}", part1(&mut intcode.clone())?)?;
writeln!(res, "part 2: {}", part2(&intcode, PART2_EXPECTED)?)?;
Ok(res)
}
fn eval(intcode: &mut [usize]) -> Result<()> {

View file

@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::error::Error;
use std::fmt::Write;
use std::str::FromStr;
use aoc::err;
@ -8,11 +9,13 @@ use aoc::Result;
const INPUT: &str = include_str!("../input/day03.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
enum Move {

View file

@ -1,13 +1,17 @@
use std::fmt::Write;
use aoc::err;
use aoc::Result;
const INPUT: &str = include_str!("../input/day04.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<usize> {

View file

@ -1,13 +1,17 @@
use std::fmt::Write;
use aoc::err;
use aoc::Result;
const INPUT: &str = include_str!("../input/day05.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<i64> {

View file

@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt::Write;
use std::iter;
use aoc::err;
@ -7,11 +8,13 @@ use aoc::Result;
const INPUT: &str = include_str!("../input/day06.txt");
pub fn run() -> Result<()> {
println!("part 1: {}", part1(INPUT)?);
println!("part 2: {}", part2(INPUT)?);
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
Ok(())
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn count_orbits(

View file

@ -1,3 +1,4 @@
use aoc::DayFunc;
use aoc::Result;
use aoc2019::day01;
@ -8,7 +9,7 @@ use aoc2019::day05;
use aoc2019::day06;
fn main() -> Result<()> {
let days: &[fn() -> Result<()>] = &[
let days: &[DayFunc] = &[
day01::run,
day02::run,
day03::run,