aboutsummaryrefslogtreecommitdiff
path: root/challenge-014/paulo-custodio/c/ch-2.c
blob: 250f3d6351198d1d6ea56f641a717b99aef96128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Challenge 014

Challenge #2
Using only the official postal (2-letter) abbreviations for the 50 U.S.
states, write a script to find the longest English word you can spell? Here
is the list of U.S. states abbreviations as per wikipedia page. This challenge
was proposed by team member Neil Bowers.

For example,
Pennsylvania + Connecticut = PACT
Wisconsin + North Dakota = WIND
Maine + Alabama = MEAL
California + Louisiana + Massachusetts + Rhode Island = Calamari
*/

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "utstring.h"
#include "utarray.h"
#include "uthash.h"

struct us_states {
    const char* key;
    const char* name;
    UT_hash_handle hh;
};

struct us_states lut_us_states[] = {
    { "AL", "Alabama" },
    { "AK", "Alaska" },
    { "AZ", "Arizona" },
    { "AR", "Arkansas" },
    { "CA", "California" },
    { "CO", "Colorado" },
    { "CT", "Connecticut" },
    { "DE", "Delaware" },
    { "FL", "Florida" },
    { "GA", "Georgia" },
    { "HI", "Hawaii" },
    { "ID", "Idaho" },
    { "IL", "Illinois" },
    { "IN", "Indiana" },
    { "IA", "Iowa" },
    { "KS", "Kansas" },
    { "KY", "Kentucky" },
    { "LA", "Louisiana" },
    { "ME", "Maine" },
    { "MD", "Maryland" },
    { "MA", "Massachusetts" },
    { "MI", "Michigan" },
    { "MN", "Minnesota" },
    { "MS", "Mississippi" },
    { "MO", "Missouri" },
    { "MT", "Montana" },
    { "NE", "Nebraska" },
    { "NV", "Nevada" },
    { "NH", "New Hampshire" },
    { "NJ", "New Jersey" },
    { "NM", "New Mexico" },
    { "NY", "New York" },
    { "NC", "North Carolina" },
    { "ND", "North Dakota" },
    { "OH", "Ohio" },
    { "OK", "Oklahoma" },
    { "OR", "Oregon" },
    { "PA", "Pennsylvania" },
    { "RI", "Rhode Island" },
    { "SC", "South Carolina" },
    { "SD", "South Dakota" },
    { "TN", "Tennessee" },
    { "TX", "Texas" },
    { "UT", "Utah" },
    { "VT", "Vermont" },
    { "VA", "Virginia" },
    { "WA", "Washington" },
    { "WV", "West Virginia" },
    { "WI", "Wisconsin" },
    { "WY", "Wyoming" },
    { NULL, NULL },
};

struct us_states* lut_states = NULL;

void build_lookup_table() {
    for (struct us_states* p = lut_us_states; p->key != NULL; p++)
        HASH_ADD_KEYPTR(hh, lut_states, p->key, strlen(p->key), p);
}

const char* lookup_state(const char* str, size_t len) {
    UT_string* str_upper;
    utstring_new(str_upper);
    for (const char* p = str; *p && utstring_len(str_upper) < len; p++)
        utstring_printf(str_upper, "%c", toupper(*p));

    struct us_states* elt;
    HASH_FIND_STR(lut_states, utstring_body(str_upper), elt);

    utstring_free(str_upper);
    return elt ? elt->name : NULL;
}

bool state_words(UT_string* result, const char* word) {
    utstring_clear(result);

    if (strlen(word) % 2 != 0)          // odd number of chars
        return false;

    for (int i = 0; i < strlen(word); i += 2) {
        const char* state = lookup_state(word + i, 2);
        if (state == NULL)
            return false;               // no state found
        utstring_printf(result, "%s%s", i == 0 ? "" : " + ", state);
    }

    return true;
}

char* chomp(char* str) {
    char* p = str + strlen(str) - 1;
    while (p >= str && isspace(*p))
        *p = '\0';
    return str;
}

UT_array* search_words(const char* dictionary) {
    FILE* fp = fopen(dictionary, "r");
    if (fp == NULL) {
        perror(dictionary);
        exit(EXIT_FAILURE);
    }

    // array to return list
    UT_array* words;
    utarray_new(words, &ut_str_icd);

    UT_string* text;
    utstring_new(text);

    size_t curlen = 0;

    char word[BUFSIZ];
    while (fgets(word, sizeof(word), fp)) {
        chomp(word);
        if (state_words(text, word)) {
            if (strlen(word) > curlen) {
                utarray_clear(words);
                char* elt = word;
                utarray_push_back(words, &elt);
                curlen = strlen(word);
            }
            else if (strlen(word) == curlen) {
                char* elt = word;
                utarray_push_back(words, &elt);
            }
        }
    }

    fclose(fp);
    utstring_free(text);
    return words;
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        fputs("usage: ch-2 dictionary.txt\n", stderr);
        return EXIT_FAILURE;
    }

    build_lookup_table();
    UT_string* text;
    utstring_new(text);

    UT_array* words = search_words(argv[1]);
    for (char** p = NULL; (p = (char**)utarray_next(words, p)) != NULL; ) {
        if (state_words(text, *p))
            printf("%s = %s\n", *p, utstring_body(text));
    }

    utarray_free(words);
    utstring_free(text);
}