init
This commit is contained in:
6
problems/p22/Cargo.toml
Normal file
6
problems/p22/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "p22"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
1
problems/p22/p022_names.txt
Normal file
1
problems/p22/p022_names.txt
Normal file
File diff suppressed because one or more lines are too long
23
problems/p22/src/main.rs
Normal file
23
problems/p22/src/main.rs
Normal 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(())
|
||||
}
|
||||
Reference in New Issue
Block a user