advent_of_code_2022/aoc_03b/src/main.rs

60 lines
1.9 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("../aoc_03a/input");
let mut result = 0;
let mut i = 0;
let mut hashmap = HashMap::new();
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 i == 0 {
hashmap = HashMap::new();
}
if let Ok(ip) = line {
println!("{:?}", hashmap);
for c in ip.chars() {
if i == 0 {
hashmap.insert(c, 100);
}
else if i == 1 {
*hashmap.entry(c).or_insert(2) += 1;
}
else if i == 2 {
match hashmap.get(&c) {
Some(&number) => {
if number > 100 {
println!("{}", c);
if c >= 'A' && c <= 'Z' {
result += 27 + c as u32 - 'A' as u32;
}
else {
result += 1 + c as u32 - 'a' as u32;
}
break;
}
}
_ => (),
}
}
}
}
i = (i + 1) % 3;
}
}
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())
}