-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
87 lines (71 loc) · 1.83 KB
/
lib.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
78
79
80
81
82
83
84
85
86
87
extern crate core;
use std::collections::HashMap;
pub fn fn1(input: &str) -> String {
let lines: Vec<_> = input.lines().collect();
let l = lines[0].len();
let mut res = String::new();
for i in 0..l {
let mut count = HashMap::new();
for s in &lines {
let x = count.entry(s.chars().nth(i).unwrap()).or_insert(0);
*x += 1;
}
let mut max = 0;
let mut char = ' ';
count.iter().for_each(|(c, &v)| {
if v > max {
max = v;
char = *c;
}
});
res.push(char);
}
res
}
pub fn fn2(input: &str) -> String {
let lines: Vec<_> = input.lines().collect();
let l = lines[0].len();
let mut res = String::new();
for i in 0..l {
let mut count = HashMap::new();
for s in &lines {
let x = count.entry(s.chars().nth(i).unwrap()).or_insert(0);
*x += 1;
}
let mut min = std::i32::MAX;
let mut char = ' ';
count.iter().for_each(|(c, &v)| {
if v < min {
min = v;
char = *c;
}
});
res.push(char);
}
res
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_fn1_unit() {
let s = fs::read_to_string("test.txt").unwrap();
assert_eq!(fn1(s.as_str()), "easter");
}
#[test]
fn test_fn1_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(fn1(s.as_str()), "");
}
#[test]
fn test_fn2_unit() {
let s = fs::read_to_string("test.txt").unwrap();
assert_eq!(fn2(s.as_str()), "advent");
}
#[test]
fn test_fn2_input() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(fn2(s.as_str()), "advent");
}
}