1
0
This commit is contained in:
2026-02-04 11:15:03 +08:00
commit 8b20a5dd21
125 changed files with 4177 additions and 0 deletions

6
problems/p22/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "p22"
version = "0.1.0"
edition = "2024"
[dependencies]

File diff suppressed because one or more lines are too long

23
problems/p22/src/main.rs Normal file
View File

@@ -0,0 +1,23 @@
use std::fs::File;
use std::io::{BufRead, BufReader};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = File::open("problems/p22/p022_names.txt")?;
let mut reader = BufReader::new(file);
let mut line = String::new();
reader.read_line(&mut line)?;
let cleaned_line = line.replace("\"", "");
let mut names: Vec<&str> = cleaned_line.split(',').collect();
names.sort();
let mut sum = 0;
for (idx, name) in names.iter().enumerate() {
let alpha_score: u32 = name
.chars()
.filter(|c| c.is_ascii_alphabetic())
.map(|c| c.to_ascii_lowercase() as u32 - 96)
.sum();
sum += alpha_score * (idx as u32 + 1);
}
println!("{sum}");
Ok(())
}