Browse Source

022 ok

master
Coin de Gamma 1 year ago
parent
commit
5e4bfb7a9b
  1. 52
      022/main.rs
  2. 1
      022/names.txt

52
022/main.rs

@ -0,0 +1,52 @@
use std::fs::File;
use std::io::prelude::*;
fn ascore(s: &str) -> u32 {
let sr = s.replace("\"","");
let chars = sr.chars();
//println!("------>>>>{}", sr);
let mut sum = 0u32;
for (i, c) in sr.chars().enumerate() {
//println!(":[{}];", c);
let dd = (c as u32) - 64;
sum += dd;
//println!("{} {} {}", i, c, dd);
}
return sum;
}
fn main() {
//let name = "COLIN";
//let score = ascore(name);
let mut file = File::open("names.txt").unwrap();
let mut content = String::new();
file.read_to_string(&mut content).expect("cant read file");
let names_split = content.split(",");
let mut names: Vec<String> = Vec::new();
for name in names_split {
names.push(String::from(name));
}
names.sort();
//println!("{}", content);
let mut i = 1u32;
let mut sum = 0;
for nm in names {
let name = nm.as_str();
let score = ascore(name) * i;
sum += score;
println!("{} [{}, {}, {}]", name, ascore(name), i, ascore(name) * i);
i += 1;
//if i > 10 {
// break;
//}
//println!("{}", name);
}
//println!("len: ", names.len());
println!("score: {}", sum);
}

1
022/names.txt

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save