-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_2016_06.rs
77 lines (60 loc) · 1.74 KB
/
solution_2016_06.rs
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
use advent_of_code_common::parsing::{Error, parse_lines_to_vec};
use advent_of_code_common::utils::{least_frequent, most_frequent, transpose};
type Data = Vec<String>;
fn parse(input: &str) -> Result<Data, Error> {
parse_lines_to_vec(input)
}
fn solve<F>(data: &Data, f: F) -> String
where
F: Fn(&[char]) -> Option<&char>,
{
let chars: Vec<Vec<char>> = data.iter().map(|s| s.chars().collect()).collect();
let transposed = transpose(&chars);
transposed
.iter()
.map(|chars| {
f(chars).unwrap_or_else(|| panic!("Most frequent element not found in {chars:?}!"))
})
.collect()
}
fn solve_1(data: &Data) -> String {
solve(data, most_frequent)
}
fn solve_2(data: &Data) -> String {
solve(data, least_frequent)
}
fn part_1(input: &str) -> Result<String, Error> {
parse(input).map(|input| solve_1(&input))
}
fn part_2(input: &str) -> Result<String, Error> {
parse(input).map(|input| solve_2(&input))
}
const DATA: &str = include_str!("../../resources/06.txt");
fn main() -> Result<(), Error> {
let result_1 = part_1(DATA)?;
println!("Part 1: {result_1}");
let result_2 = part_2(DATA)?;
println!("Part 2: {result_2}");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = include_str!("../../resources/06-test.txt");
#[test]
fn test_solve_1_test() {
assert_eq!(part_1(TEST), Ok("easter".to_string()));
}
#[test]
fn test_solve_1_real() {
assert_eq!(part_1(DATA), Ok("mshjnduc".to_string()));
}
#[test]
fn test_solve_2_test() {
assert_eq!(part_2(TEST), Ok("advent".to_string()));
}
#[test]
fn test_solve_2_real() {
assert_eq!(part_2(DATA), Ok("apfeeebz".to_string()));
}
}