51 lines
1.5 KiB
Rust
51 lines
1.5 KiB
Rust
use std::fs::File;
|
|
use std::io::{self, BufRead};
|
|
use std::path::Path;
|
|
use std::collections::HashMap;
|
|
|
|
fn main() {
|
|
let file_path = String::from("input");
|
|
let mut result = 0;
|
|
|
|
println!("In file {}", file_path);
|
|
|
|
if let Ok(lines) = read_lines(file_path) {
|
|
// Consumes the iterator, returns an (Optional) String
|
|
for line in lines {
|
|
if let Ok(ip) = line {
|
|
let len = ip.len();
|
|
let mut i = 0;
|
|
let mut hashmap = HashMap::new();
|
|
for c in ip.chars() {
|
|
if i < len / 2 {
|
|
hashmap.insert(c, Some(0));
|
|
}
|
|
else {
|
|
match hashmap.get(&c) {
|
|
Some(&_number) => {
|
|
if c >= 'A' && c <= 'Z' {
|
|
result += 27 + c as u32 - 'A' as u32;
|
|
}
|
|
else {
|
|
result += 1 + c as u32 - 'a' as u32;
|
|
}
|
|
break;
|
|
}
|
|
_ => result += 0,
|
|
}
|
|
}
|
|
i += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
println!("{}", result);
|
|
}
|
|
|
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
|
where P: AsRef<Path>, {
|
|
let file = File::open(filename)?;
|
|
Ok(io::BufReader::new(file).lines())
|
|
}
|