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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,3 +1,4 @@
use aoc::DayFunc;
use aoc::Result; use aoc::Result;
use aoc2018::day01; use aoc2018::day01;
@ -7,7 +8,7 @@ use aoc2018::day04;
use aoc2018::day05; use aoc2018::day05;
fn main() -> Result<()> { 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) aoc::run(days)
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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