Created
October 11, 2019 01:39
-
-
Save PhilippThaler/ea3e0c867b535980ef09c72fca8724bf to your computer and use it in GitHub Desktop.
Reddit Daily Programmer #380
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.reddit.com/r/dailyprogrammer/comments/cmd1hb/20190805_challenge_380_easy_smooshed_morse_code_1/ | |
import java.io.IOException; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.*; | |
public class Main { | |
private static final String[] morseAlphabet = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..".split(" "); | |
private static final String alphabet = "abcdefghijklmnopqrstuvwxyz"; | |
public static String parseWord(String word) { | |
StringBuilder morseString = new StringBuilder(); | |
for (char ch : word.toCharArray()) { | |
int index = alphabet.indexOf(ch); | |
morseString.append(morseAlphabet[index]); | |
} | |
return morseString.toString(); | |
} | |
private static List<String> readWordlist() throws IOException { | |
List<String> words = new ArrayList<>(); | |
Path wordsFile = Paths.get("./src/wordlist.txt"); | |
Scanner scanner = new Scanner(wordsFile); | |
while (scanner.hasNextLine()) { | |
words.add(scanner.nextLine()); | |
} | |
return words; | |
} | |
public static void main(String[] args) { | |
List<String> wordList = null; | |
try { | |
wordList = readWordlist(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | |
System.out.println("Find the only sequence that's the code for 13 different words."); | |
Map<String, List<String>> wordMap = new HashMap<>(); | |
for (String word : wordList) { | |
String morseWord = parseWord(word); | |
if (wordMap.containsKey(morseWord)) { | |
List<String> words = new ArrayList<>(wordMap.get(morseWord)); | |
words.add(word); | |
wordMap.put(morseWord, words); | |
} else { | |
wordMap.put(morseWord, Arrays.asList(new String[]{word})); | |
} | |
} | |
for (Map.Entry<String, List<String>> entry : wordMap.entrySet()) { | |
if (entry.getValue().size() == 13) { | |
System.out.println(entry.getKey()); | |
} | |
} | |
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | |
System.out.println("Find the only word that has 15 dashes in a row"); | |
for (String word : wordList) { | |
String morseWord = parseWord(word); | |
int counter = 0; | |
int longest = 0; | |
for (char c : morseWord.toCharArray()) { | |
if (c == '-') { | |
counter++; | |
if (counter > longest) | |
longest = counter; | |
} else { | |
counter = 0; | |
} | |
} | |
if (longest == 15) { | |
System.out.println(word); | |
System.out.println(morseWord); | |
} | |
} | |
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | |
System.out.println("Find 21-letter words that are perfectly balanced"); | |
for (String word : wordList) { | |
if (word.length() == 21) { | |
String morseWord = parseWord(word); | |
int dots = 0; | |
int dashes = 0; | |
for (char c : morseWord.toCharArray()) { | |
switch (c) { | |
case '.': | |
dots++; | |
break; | |
case '-': | |
dashes++; | |
break; | |
} | |
} | |
if (dots == dashes) { | |
System.out.println(word); | |
} | |
} | |
} | |
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | |
System.out.println("Find the only 13-letter word that encodes to a palindrome"); | |
for (String word : wordList) { | |
if (word.length() == 13) { | |
String morseWord = parseWord(word); | |
String reversed = new StringBuilder(morseWord).reverse().toString(); | |
if (morseWord.equals(reversed)) { | |
System.out.println(word); | |
System.out.println(morseWord); | |
} | |
} | |
} | |
System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | |
System.out.println("Find the 13-character sequences that don't appear in the encoding of any word"); | |
int limit = (int) Math.pow(2, 13); | |
for (int i = 0; i < limit; i++) { | |
String binary = Integer.toBinaryString(i); | |
StringBuilder builder = new StringBuilder(); | |
for (char c : binary.toCharArray()) { | |
if (c == '0') { | |
builder.append('.'); | |
} else if (c == '1') { | |
builder.append('-'); | |
} | |
} | |
int length = Integer.toBinaryString(i).length(); | |
if (length < 13) { | |
for (int j = 0; j < (13 - length); j++) { | |
builder.insert(0, '.'); | |
} | |
} | |
boolean contained = false; | |
if (builder.length() == 13) { | |
for (String key : wordMap.keySet()) { | |
if (key.contains(builder)) { | |
contained = true; | |
break; | |
} | |
} | |
if (!contained) { | |
System.out.println(builder); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment