Skip to content

Commit

Permalink
Multi-line output
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Sep 24, 2023
1 parent 08fcfb5 commit e19e7e8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
5 changes: 4 additions & 1 deletion aoc/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ where
if main_only && version.is_some() {
continue;
}
let results_start = results.chars().count();
write!(&mut results, "Day {day} - part {part}")?;
if let Some(version) = version {
write!(&mut results, " — {version}")?;
}
let before = chrono::Utc::now();
let result = runner()?;
let after = chrono::Utc::now();
write!(&mut results, ": {result}")?;
if timings {
write!(&mut results, " ({})", pretty_duration(after - before))?;
}
write!(&mut results, ": ")?;
let sep = format!("\n{}", " ".repeat(results.chars().count() - results_start));
write!(&mut results, "{}", result.trim().replace('\n', &sep))?;
writeln!(&mut results)?;
}
}
Expand Down
16 changes: 13 additions & 3 deletions aoc/tests/dummy-year.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ fn day1_main() {
fn day2() {
insta::assert_snapshot!(run_with(&["-d", "2"]), @r###"
Day 2 - part 1: 1606483
Day 2 - part 2: 3842356
Day 2 - part 2: 20x3x11
15x27x5
Day 2 - part 2 — no_eol: 20x3x11
15x27x5
"###);
}

Expand All @@ -51,7 +55,11 @@ fn all_days() {
Day 1 - part 2 — result: 1783
Day 1 - part 2 — result_string: 1783
Day 2 - part 1: 1606483
Day 2 - part 2: 3842356
Day 2 - part 2: 20x3x11
15x27x5
Day 2 - part 2 — no_eol: 20x3x11
15x27x5
"###);
}

Expand All @@ -62,6 +70,8 @@ fn all_days_main() {
Day 1 - part 1: 232
Day 1 - part 2: 1783
Day 2 - part 1: 1606483
Day 2 - part 2: 3842356
Day 2 - part 2: 20x3x11
15x27x5
"###);
}
5 changes: 4 additions & 1 deletion dummy-year/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ Day 1 - part 2: 1783
Day 1 - part 2 — result: 1783
Day 1 - part 2 — result_string: 1783
Day 2 - part 1: 1606483
Day 2 - part 2: 3842356
Day 2 - part 2: 20x3x11
15x27x5
Day 2 - part 2 — no_eol: 20x3x11
15x27x5
17 changes: 7 additions & 10 deletions dummy-year/src/day2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ fn part1(input: &str) -> u32 {
}

#[aoc(day2, part2)]
fn part2(input: &str) -> u32 {
generator(input)
.unwrap()
.iter()
.map(|v| {
let mut v = v.clone();
v.sort_unstable();
(v[0] + v[1]) * 2 + v[0] * v[1] * v[2]
})
.sum()
fn part2(input: &[&str]) -> String {
format!("{}\n{}\n", input[0], input[1])
}

#[aoc(day2, part2, no_eol)]
fn part2_no_eol(input: &[&str]) -> String {
format!("{}\n{}", input[0], input[1])
}

0 comments on commit e19e7e8

Please sign in to comment.