previndexinfo

code guessing, round #71 (completed)

started at ; stage 2 at ; ended at

specification

hi again! your challenge is to huffman code. submissions may be written in any language.

Huffman coding is a type of optimal prefix code that assigns variable-length binary words to input symbols based on their frequencies.

given a mapping from symbols to frequencies representing how often those symbols appear, we can construct an optimal code representing each symbol with a binary string ("codeword"), such that no codeword is a prefix of any other and that the expected length of the corresponding codeword is minimal when picking a random symbol according to the provided distribution.

once this mapping, the Huffman code, is created, we can use it to effectively compress messages following the distribution it is based on by simply replacing each symbol with the corresponding codeword. as the code is prefix-free, there will be no ambiguity when we decompress the result, as long as we still have the code used to compress it.

today, we are asking you to "compress a message" using this method. that means to observe the frequency that each symbol appears in the message, build a Huffman code accordingly, then use it to encode the message. since the code used depends on the content of the message encoded, a description of the code in some format must also be given in order for the compressed data to be decodable. this could be the frequency table (from which a decompressor could reconstruct the code) or a description of the code itself, or you could use another method to represent it (such as using a canonical Huffman code, which is easier to store).

your challenge, given a string of symbols, is to compress it as described above, providing the code in some format along with the encoded bitstring. as any language is allowed, there is no fixed API.

results

  1. 👑 taswelll +3 -3 = 0
    1. Dolphy
    2. oleander
    3. essaie
  2. Dolphy +3 -3 = 0
    1. oleander
    2. taswelll
    3. essaie
  3. oleander +3 -3 = 0
    1. Dolphy
    2. taswelll
    3. essaie
  4. essaie +3 -3 = 0
    1. Dolphy
    2. oleander
    3. taswelll

entries

you can download all the entries

entry #1

written by Dolphy
submitted at
0 likes

guesses
comments 0

post a comment


huffmen.c ASCII text
  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
#include <stdio.h>
#include <string.h>

#include "vector.h"

typedef struct HuffmanNode {
  char c;
  int freq;
  struct HuffmanNode *left;
  struct HuffmanNode *right;
} HuffmanNode;

DEFINE_VEC(HuffmanNode);
IMPLEMENT_VEC(HuffmanNode);

void count_freqs(const char *str, size_t len, vector_HuffmanNode *out) {
  for (size_t i = 0; i < len; ++i) {
    char c = str[i];
    int found = 0;
    FORV(i, *out) {
      HuffmanNode *node = vector_HuffmanNode_atp(out, i);
      if (node->c == c) {
        ++node->freq;
        found = 1;
      }
    }

    if (!found) {
      vector_HuffmanNode_push(
          out, (HuffmanNode){.c = c, .freq = 1, .left = NULL, .right = NULL});
    }
  }
}

void free_tree(HuffmanNode *node) {
  if (node->c == '\0') {
    free_tree(node->left);
    free_tree(node->right);
  }
  free(node);
}

void swap_nodes(HuffmanNode *const a, HuffmanNode *const b) {
  HuffmanNode temp = *a;
  *a = *b;
  *b = temp;
}

void min_heapify(vector_HuffmanNode *const vec, size_t idx) {
  size_t left = 2 * idx + 1;
  size_t right = 2 * idx + 2;
  size_t smallest = idx;

  if (left < vec->len && vector_HuffmanNode_at(vec, left).freq <
                             vector_HuffmanNode_at(vec, smallest).freq)
    smallest = left;
  if (right < vec->len && vector_HuffmanNode_at(vec, right).freq <
                              vector_HuffmanNode_at(vec, smallest).freq)
    smallest = right;
  if (smallest != idx) {
    swap_nodes(vector_HuffmanNode_atp(vec, idx),
               vector_HuffmanNode_atp(vec, smallest));
    min_heapify(vec, smallest);
  }
}

void build_min_heap(vector_HuffmanNode *const vec) {
  for (int i = vec->len / 2 - 1; i >= 0; --i) {
    min_heapify(vec, i);
  }
}

void build_tree(vector_HuffmanNode *const vec, HuffmanNode *out) {
  build_min_heap(vec);
  while (vec->len > 1) {
    swap_nodes(vector_HuffmanNode_atp(vec, 0),
               vector_HuffmanNode_atp(vec, vec->len - 1));
    HuffmanNode right = vector_HuffmanNode_pop(vec);
    min_heapify(vec, 0);
    swap_nodes(vector_HuffmanNode_atp(vec, 0),
               vector_HuffmanNode_atp(vec, vec->len - 1));
    HuffmanNode left = vector_HuffmanNode_pop(vec);
    min_heapify(vec, 0);
    HuffmanNode *rightp = malloc(sizeof(HuffmanNode));
    HuffmanNode *leftp = malloc(sizeof(HuffmanNode));
    *rightp = right;
    *leftp = left;

    HuffmanNode new = (HuffmanNode){
        .c = '\0',
        .freq = left.freq + right.freq,
        .left = leftp,
        .right = rightp,
    };
    vector_HuffmanNode_push(vec, new);
    size_t i = vec->len - 1;
    while (i > 0) {
      size_t parent = (i - 1) / 2;
      if (vector_HuffmanNode_at(vec, i).freq >=
          vector_HuffmanNode_at(vec, parent).freq)
        break;
      swap_nodes(vector_HuffmanNode_atp(vec, i),
                 vector_HuffmanNode_atp(vec, parent));
      i = parent;
    }
    *out = new;
  }
}

void generate_table(HuffmanNode *const node, char **const table, char *text) {
  if (node->c != '\0') {
    size_t len = strlen(text);
    table[node->c] = malloc((len + 1) * sizeof(char));
    strncpy(table[node->c], text, len);
    table[node->c][len] = '\0';
    return;
  }

  size_t len = strlen(text);
  char *left_text = malloc((len + 2) * sizeof(char));
  char *right_text = malloc((len + 2) * sizeof(char));

  strncpy(left_text, text, len);
  strncpy(right_text, text, len);
  left_text[len] = '0';
  right_text[len] = '1';
  left_text[len + 1] = right_text[len + 1] = '\0';
  generate_table(node->left, table, left_text);
  generate_table(node->right, table, right_text);
  free(left_text);
  free(right_text);
}

void entry(char *const message, char **const out_encoded,
           char ***const out_table, size_t *out_len) {
  vector_HuffmanNode frequencies;
  vector_HuffmanNode_init(&frequencies);

  size_t message_len = strlen(message);
  count_freqs(message, message_len, &frequencies);

  HuffmanNode *tree = malloc(sizeof(HuffmanNode));
  build_tree(&frequencies, tree);

  char *text = malloc(sizeof(char));
  text = "\0";
  char *table[256] = {0};
  generate_table(tree, table, text);

  // encode
  char output[16384] = {0};
  size_t total_len = 0;
  for (size_t i = 0; i < message_len; ++i) {
    char c = message[i];
    size_t current_len = strlen(table[c]);
    strncpy(output + total_len, table[c], current_len);
    total_len += current_len;
  }
  output[total_len] = '\0';

  vector_HuffmanNode_free(&frequencies);
  *out_encoded = output;
  *out_table = table;
  *out_len = total_len;
  free_tree(tree);
}
vector.h ASCII text
 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
#ifndef __VECTOR_H__
#define __VECTOR_H__
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#define FORV(i, vec) for (size_t i = 0; i < (vec).len; ++i)

#define DEFINE_VEC(T)                                                          \
  typedef struct {                                                             \
    T *arr;                                                                    \
    size_t len;                                                                \
    size_t cap;                                                                \
  } vector_##T;                                                                \
                                                                               \
  void vector_##T##_init(vector_##T *const);                                   \
  void vector_##T##_free(vector_##T *const);                                   \
  void vector_##T##_push(vector_##T *const, T);                                \
  void vector_##T##_insert(vector_##T *const, T, size_t);                      \
  T vector_##T##_at(vector_##T *const, size_t);                                \
  T *vector_##T##_atp(vector_##T *const, size_t);                              \
  T vector_##T##_pop(vector_##T *const);

#define IMPLEMENT_VEC(T)                                                       \
  void vector_##T##_init(vector_##T *const vec) {                              \
    vec->arr = NULL;                                                           \
    vec->len = 0;                                                              \
    vec->cap = 0;                                                              \
  }                                                                            \
                                                                               \
  void vector_##T##_free(vector_##T *const vec) { free(vec->arr); }            \
                                                                               \
  void vector_##T##_push(vector_##T *const vec, T x) {                         \
    if (vec->len >= vec->cap) {                                                \
      vec->cap = (vec->cap ? (vec->cap * 2) : 8);                              \
      vec->arr = realloc(vec->arr, vec->cap * sizeof(T));                      \
    }                                                                          \
                                                                               \
    vec->arr[vec->len] = x;                                                    \
    ++vec->len;                                                                \
  }                                                                            \
                                                                               \
  void vector_##T##_insert(vector_##T *const vec, T item, size_t pos) {        \
    if (pos > vec->len + 1)                                                    \
      return;                                                                  \
    if (pos == vec->len) {                                                     \
      vector_##T##_push(vec, item);                                            \
      return;                                                                  \
    }                                                                          \
                                                                               \
    if (vec->len >= vec->cap) {                                                \
      vec->cap = (vec->cap ? (vec->cap * 2) : 8);                              \
      vec->arr = realloc(vec->arr, vec->cap * sizeof(T));                      \
    }                                                                          \
                                                                               \
    for (size_t i = vec->len - 1; i >= pos; --i) {                             \
      vec->arr[i + 1] = vec->arr[i];                                           \
    }                                                                          \
    vec->arr[pos] = item;                                                      \
    ++vec->len;                                                                \
  }                                                                            \
                                                                               \
  T vector_##T##_at(vector_##T *const vec, size_t idx) {                       \
    return *vector_##T##_atp(vec, idx);                                        \
  }                                                                            \
                                                                               \
  T *vector_##T##_atp(vector_##T *const vec, size_t idx) {                     \
    assert(idx < vec->len);                                                    \
                                                                               \
    return &vec->arr[idx];                                                     \
  }                                                                            \
                                                                               \
  T vector_##T##_pop(vector_##T *const vec) {                                  \
    T elem = vector_##T##_at(vec, vec->len - 1);                               \
    --vec->len;                                                                \
    if (vec->cap > 8 && vec->len < vec->cap / 2) {                             \
      vec->cap /= 2;                                                           \
      vec->arr = (T *)realloc(vec->arr, vec->cap * sizeof(T));                 \
    }                                                                          \
                                                                               \
    return elem;                                                               \
  }

#endif // !__VECTOR_H__

entry #2

written by oleander
submitted at
0 likes

guesses
comments 0

post a comment


codeguessing71_huffman.py ASCII text, with CRLF line terminators
 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
coded_message = "mrrp mrrrp meow meow mew meow :3"

frequency_dict = {}

for character in coded_message:
    if character in frequency_dict:
        frequency_dict[character]+= 1
    else:
        frequency_dict[character]= 1

sorted_key = []
sorted_val = []
for character in frequency_dict:
    pointer = 0
    for i in sorted_key:
        if frequency_dict[i] < frequency_dict[character]:
            pointer+=1
        else:
            break
    sorted_key.insert(pointer, character)
    sorted_val.insert(pointer, frequency_dict[character])


encoded_key = {i:"" for i in frequency_dict}


for i in range(0, len(frequency_dict)-1):
    first_key = sorted_key.pop(0)
    first_val = sorted_val.pop(0)
    
    second_key = sorted_key.pop(0)
    second_val = sorted_val.pop(0)
    
    for character in second_key:
        encoded_key[character]="0"+encoded_key[character]
    
    for character in first_key:
        encoded_key[character]="1"+encoded_key[character]
    
    new_key = first_key+second_key
    new_val = first_val+second_val
    pointer = 0
    
    for i in sorted_val:
        if i < new_val:
            pointer+=1
        else:
            break
    sorted_key.insert(pointer, new_key)
    sorted_val.insert(pointer, new_val)
    
encoded_message = ""
for character in coded_message:
    encoded_message += encoded_key[character]

print(encoded_message)

for character in encoded_key:
    print(character + "\t" + encoded_key[character])

entry #3

written by taswelll
submitted at
1 like

guesses
comments 0

post a comment


huffwoman.asm ASCII text
  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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
entry:
    pop r30  ; terminal stream pointer
    pop r0
    pop r1  ; unused
    pop r1  ; unused
    pop r1  ; unused
    cmp r0, 0
    ifz mov r0, default_filename
    mov r1, 1    ; read from our own disk
    mov r2, file_struct
    call open
    cmp r0, 0
    ifnz jmp no_error_read
    mov r0, 20
    mov r1, r30
    mov r2, msg_error_read
    call write
    call end_current_task
no_error_read:
    mov r0, file_struct
    call get_size
    mov r3, r0         ; r3: file size left
read_more:
    call read_to_buf
    mov r0, buffer
    mov r31, r4
count_loop:
    mov.8 r1, [r0]     ; r1 = char
    and r1, 0x7F
    sla r1, 2
    add r1, arr
    inc [r1]

    inc r0
    loop count_loop

    cmp r3, 0
    ifnz jmp read_more

; FREQUENCY ANALYSIS
    mov r5, 0     ; r5 = current character
    mov r6, arr   ; r6 = pointer to end of queue
    mov r31, 128
add_queue_loop:
    mov r1, r5
    sla r1, 2
    add r1, arr
    mov r0, [r1]  ; read frequency
    cmp r0, 0
    sla r0, 8
    ifz jmp no_add
    or r0, r5     ; frequency + character
    call insert
no_add:
    inc r5
    loop add_queue_loop

; HUFFMAN TREE
tree_loop:
    mov r0, [arr]
    call extract  ; pop

    cmp arr, r6
    ifz jmp tree_finished

    mov r1, [arr]
    call extract  ; pop
    mov r10, r0
    and r10, 0xff
    xor r0, r10
    mov r11, r1
    and r11, 0xff
    xor r1, r11
    sla r11, 8
    or  r10, r11  ; r10 = combined node
    add r0, r1    ; sum of probabilities << 8
    mov r1, r6
    add r1, 4     ; pointer to new node (leaving 1 empty to insert later)
    mov [r1], r10
    sub r1, arr
    sra r1, 2     ; index of new node (positive)
    or  r1, 0x80
    or  r0, r1    ; full item
    call insert
    jmp tree_loop
tree_finished:
    ; last node at r0 which should have frequency = sample length
    and r0, 0xff
    mov r7, 0     ; sequence length
    mov r8, 0     ; sequence
    call traverse
    
; ENCODING
    mov r9, 39
    mov r0, 0
    mov r1, file_struct
    call seek
    ; same loop as above really
    mov r0, file_struct
    call get_size
    mov r3, r0          ; r3: file size left
read_more2:
    call read_to_buf
    mov r0, buffer
    mov r31, r4
encode_loop:
    movz.8 r1, [r0]     ; r1 = char
    sla r1, 3
    add r1, code
    mov r7, [r1]
    mov r8, [r1+4]
    call print_binary
    inc r0
    loop encode_loop
    cmp r3, 0
    ifnz jmp read_more2
    call print_nl
    call end_current_task

; clobbers r1,r2
; inout r3: size left
; out r4: amount read
read_to_buf:
    mov r4, r3
    cmp r4, 256
    ifgt mov r4, 256
    mov r0, r4
    mov r1, file_struct
    mov r2, buffer
    call read
    sub r3, r4
    ret


; in r0: item
; clobbers r2,r3
; inout r6: pointer to end of queue
insert:
    mov [r6], r0  ; insert item to the end
    mov r2, r6
    add r6, 4
heap_up:
    ; r2 = current item. if r2 == arr it's the top node
    cmp r2, arr
    ifz ret

    mov r3, r2
    sub r3, arr
    sub r3, 4
    sra r3, 1
    and r3, 0xfffffffc
    add r3, arr   ; r3 = parent(r2)

    cmp [r3], [r2]
    iflteq ret
    xor [r2], [r3]
    xor [r3], [r2]
    xor [r2], [r3]
    mov r2, r3
    jmp heap_up

; inout r6: pointer to end of queue
; clobbers r2,r3,r4,r9
extract:
    sub r6, 4
    mov [arr], [r6]

    mov r2, arr  ; r2 = current node
down_heap:
    mov r3, r2
    sub r3, arr
    sla r3, 1
    add r3, 4
    add r3, arr  ; r3 = left child
    mov r4, r3
    add r4, 4    ; r4 = right child
    mov r9, r2   ; r9 = minimum of the above

    cmp r3, r6
    ifgteq ret  ; stop if no children
    cmp [r9], [r3]
    ifgt mov r9, r3

    cmp r4, r6
    ifgteq jmp extract_skip_r4  ; skip if r>=end
    cmp [r9], [r4]
    ifgt mov r9, r4
extract_skip_r4:
    cmp r9, r2
    ifz ret
    xor [r2], [r9]
    xor [r9], [r2]
    xor [r2], [r9]
    mov r2, r9
    jmp down_heap

; in r0: node number
; in r7: sequence length
; in r8: sequence
traverse:
    bts r0, 7
    ifz jmp traverse_leaf
    push r1
    
    xor r0, 0x80
    sla r0, 2
    add r0, arr
    mov r0, [r0]   ; r0 has left/right node
    push r0
    ; left node
    and r0, 0xff
    inc r7
    call traverse
    dec r7
    pop r0
    push r0
    ; right node
    sra r0, 8
    mov r1, 1
    sla r1, r7
    or  r8, r1
    inc r7
    call traverse
    dec r7
    
    xor r8, r1
    pop r0
    pop r1
    ret
traverse_leaf:
    push r2
    push r1
    push r0

    mov r1, r0
    sla r1, 3
    add r1, code
    mov [r1], r7
    mov [r1+4], r8

    mov r2, rsp
    mov r0, 1
    mov r1, r30
    call write
    mov r0, r8
    mov r9, 39
    call print_binary
    call print_nl

    pop r0
    pop r1
    pop r2
    ret

; in r7: sequence length
; in r8: sequence
; in r9: characters left for newline
print_binary:
    push r8
    push r7
    push r2
    push r1
    push r0
print_binary_loop:
    dec r7
    ifc jmp print_binary_stop
    mov r0, 1
    mov r1, r30
    mov r2, r8
    and r2, 1
    add r2, '0'
    push r2
    mov r2, rsp
    call write
    pop r2
    sra r8, 1

    dec r9
    ifnc jmp print_binary_loop
    call print_nl  ; newline if too far
    mov r9, 39
    jmp print_binary_loop
print_binary_stop:
    pop r0
    pop r1
    pop r2
    pop r7
    pop r8
    ret

print_nl:
    push r0
    push r1
    push r2
    mov r0, 1
    mov r1, r30
    push 10
    mov r2, rsp
    call write
    pop r2
    pop r2
    pop r1
    pop r0
    ret


arr: data.fill 0, 512   ; 128*4
code: data.fill 0, 1024 ; 128*8
file_struct: data.fill 0, 8
buffer: data.fill 0, 256

msg_error_read: data.str "Error reading file" data.8 10 data.8 0
default_filename: data.str "1:input.txt" data.8 0

#include "fox32rom.def"
#include "fox32os.def"
make ASCII text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/bin/sh

fox32asm huffwoman.asm huffwomn.fxf

$RYFS -l Huffwomn create huffwoman.img
$RYFS add huffwoman.img huffwomn.fxf
echo -n "this is an example of a huffman tree" > input.txt
$RYFS add huffwoman.img input.txt

fox32 --disk fox32os.img --disk huffwoman.img

entry #4

written by essaie
submitted at
0 likes

guesses
comments 0

post a comment


out.bin data
1
cg: couldn't decode file contents
snort.py ASCII text
 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
# (Canonical(-ish)) Huffman coder compressor by stazarzxy                         
# Usage: python3 snort.py <filename>                                              
                                                                                  
(lambda                 C,D:(lambda _,v,M:(lambda W:(lambda                 h:0 if
(print(h),                  open('out.bin','wb').write(                 bytes([int
((h+'0'*7)[i:               i+8],2)for i in range(0,len             (h),8)])))else
[])((lambda k        :D      (lambda r,l,G,h:(G,print(              k))[0]if not h
    else r(r,l,                G+k[h[0]],h[1:]),D,''               ,C))(D(lambda  
     H,l,f,h,c,V                 :dict(zip(f,h))if                 not V else(    
     lambda n:H(H                 ,l,f+[V[0][0]],                 h+[bin(n)[3:    
      ]],n,V[1:])                  )((lambda d:(                  c+1)<<d if d    
      else c+1)(V[                  0][2]+3-len                   (bin(c)))),     
       D,[],[],0,(                  lambda t:M(                  D(lambda K,L     
      ,q,p:(q+p)[0                  ]if 2>len(q                  )+len(p)else     
      (lambda e:K(                  K,L,e[2],e[                  3]+[v(e[0],e     
      [1])]))(t(q,                  p)),D,W,[]),                 lambda r,z:r     
       [2]<z[2]))(                  lambda q,p:                  (lambda l:(      
        lambda o:(                  lambda t:(o                  [0],t[0],t       
        [1],t[2]))(l                (o[1],o[2])                 ))(l(q, p))       
        )(lambda q,p:               (lambda s:(               q[0],q[1:],p)       
        if not p else(p             [0],q,p[1:]             )if not q or s(       
         q[0])>s(p[0])else          (q[0],q[1:]          ,p))(lambda n:sum        
           (c[1]for c in n))))))))(M([[(c,C.count(c),0)]for c in set(C)],         
             lambda r,z:r[0][1]<z[0][1]if r[0][1]-z[0][1]else r[0][0]<z           
               [0][0])))(__import__('sys').setrecursionlimit(len(C)*2)            
                 ,lambda l,r:(lambda s:s(l)+s(r))(lambda g:[(c[0],c[              
                    1],c[2]+1)for c in g]),lambda O,c:D(lambda m,                 
                                l,t,L:t if L<2 else                               
                                    D(lambda g,                                   
                                    l,s,d,G:G+s                                   
                                    +d if not s                                   
                                   or not d else                                  
                                   g(g,l,s[1:],d                                  
                                  ,G+[s[0]])if c                                  
                                  (s[0],d[0])else                                 
                                  g(g,l,s,d[1:],G+                                
                                 [d[0]]),D,m(m,l,t                                
                             [:L//2],L//2),m(m,l,t[L//                            
                          2:],L-L//2),[]),D,O,len(O))))(                          
                        list(open(__import__('sys').argv[1]                       
                         ).read()),lambda f,l,*n:f(f,l,*n))                       
                                                                                  
# psi kinda looks like a tree i think ^_^