Skip to content

Instantly share code, notes, and snippets.

@cubetastic33
Last active August 23, 2018 16:19
Show Gist options
  • Save cubetastic33/6cd25e5b90ac3f627671dc18751ad51b to your computer and use it in GitHub Desktop.
Save cubetastic33/6cd25e5b90ac3f627671dc18751ad51b to your computer and use it in GitHub Desktop.
Reddit Daily Programmer - Challenge #366 [Easy] Word funnel 1
//A program for the r/dailyprogrammer challenge #366 [Easy] Word Funnel 1, done using Rust.
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::time::Instant;
fn create_dictionary(filename: &str) -> io::Result<Vec<String>> {
let file = File::open(&filename)?;
let reader = io::BufReader::new(file);
let mut dictionary = vec!();
for line in reader.lines() {
let line = line?;
dictionary.append(&mut vec!(line));
}
//idk how to return just the dictionary - If I say -> Vec<String> in the declaration, it doesn't let me use the `?` operator
Ok(dictionary)
}
fn main() {
//Create the dictionary
let dictionary = match create_dictionary("enable1.txt") {
Ok(x) => x,
_ => vec!(String::from("idk how to skip this, but I know this won't ever execute")),
};
//Example for funnel
println!("Main Challenge\n{}", funnel(&String::from("reset"), &String::from("rest")));
//Example for bonus 1
println!("Bonus 1\n{:?}\nBonus 2", bonus(String::from("boats"), &dictionary));
//Bonus 2
println!("{}", bonus2(&dictionary));
}
fn funnel(word1: &String, word2: &String) -> bool {
for i in 0..word1.len() {
if word2 == &format!("{}{}", &word1[..i], &word1[i+1..]) {
return true
}
}
false
}
fn bonus(word: String, dictionary: &Vec<String>) -> Vec<String> {
let mut words = vec!();
for entry in dictionary {
if entry.len() == word.len() - 1 {
if funnel(&word, entry) == true {
words.append(&mut vec!(entry.to_string()));
}
}
}
words
}
fn bonus2(dictionary: &Vec<String>) -> usize {
let now = Instant::now();
let mut inputs = vec!();
//create letter-wise dictionary
let letters = ['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'];
let mut indexed_dictionary: Vec<Vec<String>> = vec!();
for word in dictionary {
let index: usize = letters.iter().enumerate().find(|&r| r.1.to_string() == word[0..1].to_string()).unwrap().0;
if indexed_dictionary.len() < index + 1 {
indexed_dictionary.append(&mut vec!(vec!()));
}
indexed_dictionary[index].append(&mut vec!(word.to_string()));
}
let mut words_done: usize = 0;
let mut current_custom_dict = vec!();
let mut current_custom_letters = String::from("");
for word in dictionary {
if word.len() >= 5 {
words_done += 1;
println!("Word {}: {:?}", words_done, word);
if word[..2].to_string() != current_custom_letters {
let mut custom_dictionary = vec!();
let mut first_letter = String::from("None");
for (i, letter) in word.chars().enumerate() {
if first_letter != letter.to_string() && i <= 1 {
let index: usize = letters.iter().enumerate().find(|&r| r.1.to_string() == letter.to_string()).unwrap().0;
custom_dictionary.append(&mut indexed_dictionary[index].clone());
first_letter = letter.to_string();
}
}
current_custom_letters = word[..2].to_string();
current_custom_dict = custom_dictionary;
}
let occurances = bonus(word.to_string(), &current_custom_dict);
if occurances.len() == 5 {
inputs.append(&mut vec!(word.to_string()));
println!("{:?}", inputs);
let elapsed = now.elapsed();
let sec = (elapsed.as_secs() as f64) + (elapsed.subsec_nanos() as f64 / 1000_000_000.0);
println!("Time taken: {} seconds", sec);
}
}
}
let elapsed = now.elapsed();
let sec = (elapsed.as_secs() as f64) + (elapsed.subsec_nanos() as f64 / 1000_000_000.0);
println!("Seconds: {}", sec);
println!("{:?}", inputs);
inputs.len()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment