Created
December 15, 2023 16:21
-
-
Save Alwinfy/20d9b16fa56201d383eb9269bf5bfd5f to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main() { | |
int acc = 0; | |
int buf = 0; | |
char *data = NULL; | |
char *start; | |
struct { | |
int len; | |
struct { | |
char *str; | |
int weight; | |
} lens[9]; | |
} table[256] = {0}; | |
size_t len = 0; | |
getline(&data, &len, stdin); | |
start = data; | |
for (char *c = data; *c != '\n'; c++) { | |
if (*c == ',') { | |
acc += buf; | |
buf = 0; | |
start = c + 1; | |
continue; | |
} | |
int ix = buf; | |
buf = ((buf + *c) * 17) & 255; | |
if (*c == '=' || *c == '-') { | |
int set = *c == '=' ? c[1] - '0' : -1; | |
*c = '\0'; | |
int shifted = 1; | |
for (int i = 0; i < table[ix].len; i++) { | |
if (!strcmp(table[ix].lens[i].str, start)) { | |
if (~set) { | |
table[ix].lens[i].weight = set; | |
shifted = 0; | |
} else { | |
for (int j = i + 1; j < table[ix].len; j++) { | |
table[ix].lens[j - 1] = table[ix].lens[j]; | |
} | |
--table[ix].len; | |
} | |
break; | |
} | |
} | |
if (shifted && ~set) { | |
int new_ix = table[ix].len; | |
table[ix].lens[new_ix].str = start; | |
table[ix].lens[new_ix].weight = set; | |
++table[ix].len; | |
} | |
} | |
} | |
printf("%d\n", acc + buf); | |
int power = 0; | |
for (int i = 0; i < 256; i++) { | |
int cnt = 0; | |
for (int j = 0; j < table[i].len; j++) { | |
cnt += (j + 1) * table[i].lens[j].weight; | |
} | |
power += (i + 1) * cnt; | |
} | |
printf("%d\n", power); | |
free(data); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment