Skip to content

Instantly share code, notes, and snippets.

@YoshiTheChinchilla
Created February 19, 2021 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoshiTheChinchilla/9809652dafc9708c1c26e99fd784506f to your computer and use it in GitHub Desktop.
Save YoshiTheChinchilla/9809652dafc9708c1c26e99fd784506f to your computer and use it in GitHub Desktop.
/// "2$4$8$16"
/// ↓
/// "$"を"#"に置換
/// ↓
/// "2#4#8#16"
/// ↓
/// "#"を区切り文字に配列へ分割
/// その要素数を表示
/// ↓
/// "4"
/// ↓
/// 配列に要素"32"を追加
/// その要素数を表示
/// ↓
/// "5"
/// ↓
/// 配列を整数型に変換し要素の合計値を算出
/// ↓
/// "62"
/// ↓
/// 整数変換前の配列に"++"を区切り文字とし、文字列として結合
/// ↓
/// "2++4++8++16++32"
/// ↓
/// 文字列"Hoge->"と上記文字列を連結
/// ↓
/// "Hoge->2++4++8++16++32"
/// ↓
/// 左から10文字分切り出す
/// ↓
/// "Hoge->2++4"
/// ↓
/// 右から6文字分切り出す
/// ↓
/// "->2++4"
fn solve(s: &str) -> String {
let mut result = Vec::with_capacity(9);
result.push(s.into());
let replaced = s.replace("$", "#");
result.push(replaced.clone());
let mut split = replaced.split("#").collect::<Vec<_>>();
result.push(split.len().to_string());
split.push("32");
result.push(split.len().to_string());
result.push(split.iter().flat_map(|i| i.parse::<i32>()).sum::<i32>().to_string());
let mut joined = split.join("++");
result.push(joined.clone());
joined.insert_str(0, "Hoge->");
result.push(joined.clone());
let joined = joined[0..10].to_string();
result.push(joined.clone());
result.push(joined[4..10].to_string());
result.join("\n")
}
fn main() {
let input = "2$4$8$16";
let expected = "\
2$4$8$16
2#4#8#16
4
5
62
2++4++8++16++32
Hoge->2++4++8++16++32
Hoge->2++4
->2++4";
let answer = solve(input);
assert_eq!(solve(input), expected);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment