From 161066074987cc7712694c6f71bdb27c18ab36eb Mon Sep 17 00:00:00 2001 From: Pcornat Date: Mon, 5 Dec 2022 22:04:14 +0100 Subject: [PATCH] "Modular" approach --- src/prob3/mod.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/prob3/mod.rs b/src/prob3/mod.rs index 175594a..783354d 100644 --- a/src/prob3/mod.rs +++ b/src/prob3/mod.rs @@ -1,5 +1,13 @@ use std::collections::HashSet; +fn character_to_code(input: i32) -> i32 { + if input >= ('a' as i32) { + input - 96 + } else { + input - 38 + } +} + pub fn solve_part1(file_path: &str) -> i32 { let binding = std::fs::read_to_string(file_path).expect("Please provide a text file as an argument."); @@ -12,11 +20,7 @@ pub fn solve_part1(file_path: &str) -> i32 { let second = last.chars().collect::>(); let intersection = first.chars().collect::>(); let character = *intersection.intersection(&second).next().unwrap_or(&(0 as char)) as i32; - if character >= ('a' as i32) { - character - 96 - } else { - character - 38 - } + character_to_code(character) }) .collect::>() .iter()