1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-07-18 02:20:12 +02:00

feat(solutions): add ascii-art/rust/function

This commit is contained in:
Divlo 2023-01-04 09:30:10 +01:00
parent 31d1b65153
commit b8e38c3d58
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
4 changed files with 103 additions and 0 deletions

View File

@ -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"

View File

@ -0,0 +1,6 @@
[package]
name = "rust_application"
version = "1.0.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,3 @@
# ascii-art/rust/function
Created by [@Divlo](https://github.com/Divlo) on 4 January 2023.

View File

@ -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<char, Vec<String>> = 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<String>> = 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);
}