mirror of
https://codeberg.org/JasterV/aoc_2021.git
synced 2026-04-26 18:40:05 +00:00
refactors part 1
This commit is contained in:
parent
74a20bec4f
commit
429866fdaf
1 changed files with 13 additions and 9 deletions
|
|
@ -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())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue