2022-12-05 20:23:57 +01:00
|
|
|
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.");
|
2022-12-05 21:55:28 +01:00
|
|
|
binding
|
2022-12-05 20:23:57 +01:00
|
|
|
.lines()
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.iter()
|
|
|
|
.map(|item: &&str| {
|
|
|
|
let (first, last) = item.split_at(item.len() / 2);
|
|
|
|
let second = last.chars().collect::<HashSet<char>>();
|
|
|
|
let intersection = first.chars().collect::<HashSet<char>>();
|
2022-12-05 21:55:28 +01:00
|
|
|
let character = *intersection.intersection(&second).next().unwrap() as i32;
|
|
|
|
if character >= ('a' as i32) {
|
|
|
|
character - 96
|
|
|
|
} else {
|
|
|
|
character - 38
|
|
|
|
}
|
2022-12-05 20:23:57 +01:00
|
|
|
})
|
2022-12-05 21:55:28 +01:00
|
|
|
.collect::<Vec<i32>>()
|
|
|
|
.iter()
|
|
|
|
.sum::<i32>()
|
2022-12-05 20:23:57 +01:00
|
|
|
}
|