refactors part 1

This commit is contained in:
JasterV 2021-12-03 14:48:14 +01:00
parent 74a20bec4f
commit 429866fdaf

View file

@ -14,15 +14,7 @@ fn main() -> Result<()> {
}
fn extract_report_rates(report: &[String]) -> [i32; 2] {
let sums: Vec<i32> = report.iter().fold(vec![], |sums, curr| {
curr.chars()
.enumerate()
.map(|(index, char)| {
let value = if char == '0' { -1 } else { 1 };
sums.get(index).map_or(value, |sum| sum + value)
})
.collect()
});
let sums = compute_digits_sums(report);
let gamma_rate: String = sums
.iter()
.map(|&sum| if sum > 0 { "1" } else { "0" })
@ -39,6 +31,18 @@ fn extract_report_rates(report: &[String]) -> [i32; 2] {
]
}
fn compute_digits_sums(report: &[String]) -> Vec<i32> {
report.iter().fold(vec![], |sums, curr| {
curr.chars()
.enumerate()
.map(|(index, char)| {
let value = if char == '0' { -1 } else { 1 };
sums.get(index).map_or(value, |sum| sum + value)
})
.collect()
})
}
fn read_diagnostic_report(file_path: &str) -> Result<Vec<String>> {
read_lines(file_path).map(|lines| lines.filter_map(|line| line.ok()).collect())
}