From 5661ba3f2f1550d6f9c7868532d1ea8bf63b22e6 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Mon, 5 Dec 2022 21:55:28 +0100 Subject: [PATCH] Right answer ! --- src/main.rs | 2 +- src/prob3/mod.rs | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2752061..94e251c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,6 @@ fn main() { { let file_path = "prob3_part1.txt"; let max_part1 = prob3::solve_part1(file_path); - println!("{max_part1}"); + println!("Solution for problem 3 part 1: {max_part1}"); } } diff --git a/src/prob3/mod.rs b/src/prob3/mod.rs index 1fab3aa..6c75330 100644 --- a/src/prob3/mod.rs +++ b/src/prob3/mod.rs @@ -3,7 +3,7 @@ use std::collections::HashSet; 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."); - let content = binding + binding .lines() .collect::>() .iter() @@ -11,9 +11,14 @@ pub fn solve_part1(file_path: &str) -> i32 { let (first, last) = item.split_at(item.len() / 2); let second = last.chars().collect::>(); let intersection = first.chars().collect::>(); - *intersection.intersection(&second).next().unwrap() + let character = *intersection.intersection(&second).next().unwrap() as i32; + if character >= ('a' as i32) { + character - 96 + } else { + character - 38 + } }) - .collect::>(); - - 0 + .collect::>() + .iter() + .sum::() }