Skip to content

Instantly share code, notes, and snippets.

@cubetastic33
Last active August 17, 2018 08:59
Show Gist options
  • Save cubetastic33/fb2e3825b5de512050bb85ec55c1b797 to your computer and use it in GitHub Desktop.
Save cubetastic33/fb2e3825b5de512050bb85ec55c1b797 to your computer and use it in GitHub Desktop.
Reddit Daily Programmer - Challenge #221 [Easy] Word snake
//A solution to the r/dailyprogrammer challenge #221 [Easy] Word Snake, done using Rust. Only goes right and down.
use std::io;
fn main() {
println!("Word Snake");
println!("Please enter a sentence");
let mut sentence = String::new();
io::stdin().read_line(&mut sentence)
.expect("Failed to read line");
sentence = sentence.trim().to_string();
let mut horizontal = true;
let mut first_word = true;
let mut last_length = 0;
let words: Vec<&str> = sentence.split(" ").collect();
for word in sentence.split(" ") {
if horizontal == true {
if first_word == true {
println!("{}", word);
first_word = false;
} else {
println!("{}{}", " ".repeat(last_length), word);
}
last_length += word.len()-1;
} else {
if words[words.len()-1] == word {
let word: String = word.chars().skip(1).collect();
for letter in word.chars() {
println!("{}{}", " ".repeat(last_length), letter);
}
} else {
let word: String = word.chars().skip(1).take(word.len()-2).collect();
for letter in word.chars() {
println!("{}{}", " ".repeat(last_length), letter);
}
}
}
horizontal = !horizontal;
}
}
/*
Sample output:
$ ./word_snake
Word Snake
Please enter a sentence
CAN NINCOMPOOP PANTS SCRIMSHAW WASTELAND DIRK KOMBAT TEMP PLUNGE ESTER REGRET TOMBOY
CAN
I
N
C
O
M
P
O
O
PANTS
C
R
I
M
S
H
A
WASTELAND
I
R
KOMBAT
E
M
PLUNGE
S
T
E
REGRET
O
M
B
O
Y
$ ./word_snake
Word Snake
Please enter a sentence
NICKEL LEDERHOSEN NARCOTRAFFICANTE EAT TO OATS SOUP PAST TELEMARKETER RUST THINGAMAJIG GROSS SALTPETER REISSUE ELEPHANTITIS
NICKEL
E
D
E
R
H
O
S
E
NARCOTRAFFICANTE
A
TO
A
T
SOUP
A
S
TELEMARKETER
U
S
THINGAMAJIG
R
O
S
SALTPETER
E
I
S
S
U
ELEPHANTITIS
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment