1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use itertools::EitherOrBoth::{Both, Left, Right};
use itertools::Itertools;
use std::fmt::Display;
pub fn overlap<B, F>(background: B, foreground: F) -> String
where
B: Display,
F: Display,
{
let background = background.to_string();
let foreground = foreground.to_string();
let background = background.lines();
let foreground = foreground.lines();
background
.zip_longest(foreground)
.map(|lines| {
let combined_line: String = match lines {
Both(b_line, f_line) => {
let b_chars = b_line.chars();
let f_chars = f_line.chars();
let combined_line: String = b_chars
.zip_longest(f_chars)
.map(|chars| match chars {
Both(b_char, f_char) if f_char.is_whitespace() => b_char,
Both(_, f_char) => f_char,
Left(b_char) => b_char,
Right(f_char) => f_char,
})
.collect();
combined_line
}
Left(b_line) => b_line.to_string(),
Right(f_line) => f_line.to_string(),
};
format!("{}\n", combined_line)
})
.collect()
}
#[cfg(test)]
mod tests {
use super::overlap;
use pretty_assertions::assert_eq;
#[test]
fn it_works() {
const BACKGROUND: &str = include_str!("../assets/passing/background.ascii");
const FOREGROUND: &str = include_str!("../assets/passing/foreground.ascii");
const EXPECTED: &str = include_str!("../assets/expected.ascii");
assert_eq!(overlap(BACKGROUND, FOREGROUND), EXPECTED);
}
}