mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add ascii-art/rust/function
This commit is contained in:
parent
31d1b65153
commit
b8e38c3d58
7
challenges/ascii-art/solutions/rust/function/Cargo.lock
generated
Normal file
7
challenges/ascii-art/solutions/rust/function/Cargo.lock
generated
Normal 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"
|
6
challenges/ascii-art/solutions/rust/function/Cargo.toml
Normal file
6
challenges/ascii-art/solutions/rust/function/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rust_application"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
3
challenges/ascii-art/solutions/rust/function/README.md
Normal file
3
challenges/ascii-art/solutions/rust/function/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# ascii-art/rust/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 4 January 2023.
|
87
challenges/ascii-art/solutions/rust/function/src/main.rs
Normal file
87
challenges/ascii-art/solutions/rust/function/src/main.rs
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user