Skip to content

Commit

Permalink
Add morse code solver
Browse files Browse the repository at this point in the history
  • Loading branch information
ahjmorton committed Mar 15, 2020
1 parent 0edea91 commit a7d0f2c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ADD_EXECUTABLE(password_solve password_solve.c)
ADD_EXECUTABLE(morse_code_solve morse_code_solve.c)

target_link_libraries(password_solve PRIVATE lib_password_solve)
target_link_libraries(morse_code_solve PRIVATE lib_morse_code_solve)
33 changes: 33 additions & 0 deletions cli/morse_code_solve.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "lib_morse_code_solve.h"

int main(int argc, char **argv)
{
if(argc != 2) {
return 1;
}

char * letters = argv[1];
morse_code_solve_result * results = NULL;
int rc = 0;
uint8_t result_count = morse_code_solve(
letters, strlen(letters), &results
);
if(result_count == 0) {
rc = 2;
} else {
for(uint8_t i = 0; i < result_count; i++) {
morse_code_solve_result result = results[i];
printf("%s => %s\n",
result.word, result.frequency);
}
}

if(results != NULL) {
free(results);
}
return rc;
}
14 changes: 14 additions & 0 deletions include/lib_morse_code_solve.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef HEADER_MORSE_CODE_SOLVE
#define HEADER_MORSE_CODE_SOLVE

#include <stdint.h>

typedef struct {
const char * word;
const char * frequency;
} morse_code_solve_result;

uint8_t morse_code_solve(const char * letters, uint32_t letters_length, morse_code_solve_result ** results);

#endif

6 changes: 4 additions & 2 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ADD_LIBRARY(lib_char_map char_map.c)
ADD_LIBRARY(lib_password_solve password_solve.c)
ADD_LIBRARY(lib_char_map STATIC char_map.c)
ADD_LIBRARY(lib_password_solve STATIC password_solve.c)
ADD_LIBRARY(lib_morse_code_solve STATIC morse_code_solve.c)

TARGET_LINK_LIBRARIES(lib_password_solve PRIVATE lib_char_map)
TARGET_LINK_LIBRARIES(lib_morse_code_solve PRIVATE lib_char_map)
58 changes: 58 additions & 0 deletions lib/morse_code_solve.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "lib_morse_code_solve.h"
#include "lib_char_map.h"

#define WORD_COUNT 16

static const char * WORDS[WORD_COUNT][2] = {
{"shell", "3.505"},
{"halls", "3.515"},
{"slick", "3.522"},
{"trick", "3.532"},
{"boxes", "3.535"},
{"leaks", "3.542"},
{"strobe", "3.545"},
{"bistro", "3.552"},
{"flick", "3.555"},
{"bombs", "3.565"},
{"break", "3.572"},
{"brick", "3.575"},
{"steak", "3.582"},
{"sting", "3.592"},
{"vector", "3.595"},
{"beats", "3.600"}
};

#define word_length(WORD) strnlen(WORD, 7)

uint8_t morse_code_solve(const char * letters, uint32_t letters_length, morse_code_solve_result ** results) {
uint8_t result = 0;
char_map * letters_char_map = init_char_map();
pop_char_map(letters_char_map, letters, letters_length);
char_map * word_char_map = init_char_map();
for(int i = 0; i < WORD_COUNT; i++) {
clear_char_map(word_char_map);
const char * word = WORDS[i][0];
const size_t length = word_length(word);
pop_char_map(word_char_map, word, length);
uint8_t c;
for(c = 0; c < letters_length; c++) {
const char ch = letters[c];
if(count_for(letters_char_map, ch) > count_for(word_char_map, ch)) {
break;
}
}
if(c == letters_length) {
result++;
(*results) = realloc(*results, result * sizeof(morse_code_solve_result));
(*results)[result - 1].word = word;
(*results)[result - 1].frequency = WORDS[i][1];
}
}
free_char_map(letters_char_map);
free_char_map(word_char_map);
return result;
}

0 comments on commit a7d0f2c

Please sign in to comment.