From b8e38c3d58e4e4f870246c9111d0283baa534263 Mon Sep 17 00:00:00 2001 From: Divlo Date: Wed, 4 Jan 2023 09:30:10 +0100 Subject: [PATCH] feat(solutions): add `ascii-art/rust/function` --- .../solutions/rust/function/Cargo.lock | 7 ++ .../solutions/rust/function/Cargo.toml | 6 ++ .../solutions/rust/function/README.md | 3 + .../solutions/rust/function/src/main.rs | 87 +++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 challenges/ascii-art/solutions/rust/function/Cargo.lock create mode 100644 challenges/ascii-art/solutions/rust/function/Cargo.toml create mode 100644 challenges/ascii-art/solutions/rust/function/README.md create mode 100644 challenges/ascii-art/solutions/rust/function/src/main.rs diff --git a/challenges/ascii-art/solutions/rust/function/Cargo.lock b/challenges/ascii-art/solutions/rust/function/Cargo.lock new file mode 100644 index 0000000..c8ae10e --- /dev/null +++ b/challenges/ascii-art/solutions/rust/function/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust_application" +version = "1.0.0" diff --git a/challenges/ascii-art/solutions/rust/function/Cargo.toml b/challenges/ascii-art/solutions/rust/function/Cargo.toml new file mode 100644 index 0000000..a119ace --- /dev/null +++ b/challenges/ascii-art/solutions/rust/function/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rust_application" +version = "1.0.0" +edition = "2021" + +[dependencies] diff --git a/challenges/ascii-art/solutions/rust/function/README.md b/challenges/ascii-art/solutions/rust/function/README.md new file mode 100644 index 0000000..ac88df5 --- /dev/null +++ b/challenges/ascii-art/solutions/rust/function/README.md @@ -0,0 +1,3 @@ +# ascii-art/rust/function + +Created by [@Divlo](https://github.com/Divlo) on 4 January 2023. diff --git a/challenges/ascii-art/solutions/rust/function/src/main.rs b/challenges/ascii-art/solutions/rust/function/src/main.rs new file mode 100644 index 0000000..a222987 --- /dev/null +++ b/challenges/ascii-art/solutions/rust/function/src/main.rs @@ -0,0 +1,87 @@ +use std::{collections::HashMap, io}; + +fn main() { + let mut width = String::new(); + io::stdin() + .read_line(&mut width) + .expect("Failed to read `stdin` line."); + let width: usize = width + .trim() + .parse() + .expect("Failed to convert `width` as an `usize`."); + + let mut height = String::new(); + io::stdin() + .read_line(&mut height) + .expect("Failed to read `stdin` line."); + let height: usize = height + .trim() + .parse() + .expect("Failed to convert `height` as an `usize`."); + + let mut text = String::new(); + io::stdin() + .read_line(&mut text) + .expect("Failed to read `stdin` line."); + let text = text.trim(); + + let characters = [ + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', + 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '?', + ]; + + // "ABCDEFGHIJKLMNOPQRSTUVWXYZ?" Represented in ASCII art. + // key = character ; value = character represented in ASCII art line by line + let mut characters_ascii: HashMap> = HashMap::new(); + + for character in characters { + let value = vec![]; + characters_ascii.insert(character, value); + } + + for _ in 0..height { + let mut row = String::new(); + io::stdin() + .read_line(&mut row) + .expect("Failed to read `stdin` line."); + let mut character_index: usize = 0; + let mut character_ascii_index: usize = 0; + let mut row_ascii = String::new(); + for (_, character) in row.char_indices() { + let is_last = character_ascii_index == width - 1; + if is_last { + row_ascii.push(character); + let row_ascii_value = row_ascii.clone(); + row_ascii.clear(); + characters_ascii + .get_mut(&characters[character_index]) + .unwrap() + .push(row_ascii_value); + character_index += 1; + character_ascii_index = 0; + } else { + row_ascii.push(character); + character_ascii_index += 1; + } + } + } + + let mut characters_needed: Vec> = vec![]; + for (_, character) in text.char_indices() { + let mut character = character.to_uppercase(); + let mut character = character.next().unwrap(); + if !characters_ascii.contains_key(&character) { + character = '?'; + } + characters_needed.push(characters_ascii.get(&character).unwrap().clone()); + } + + let mut text_ascii = String::new(); + for column in 0..height { + for row in 0..characters_needed.len() { + text_ascii.push_str(&characters_needed[row][column]); + } + text_ascii.push('\n'); + } + println!("{}", text_ascii); +}