Day 10/C: Add support for huge inputs
This commit is contained in:
parent
545532b826
commit
9a00ee5a90
1 changed files with 38 additions and 32 deletions
|
@ -1,38 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
char C[]={'(',')','[',']','{',
|
||||
'}', '<' , '>' };int X[]={3,57,1197,
|
||||
25137} ;int Q(const void*A,const void*B){
|
||||
size_t a=*(size_t*)A,b=*(size_t*)B;return a
|
||||
>b?a<b?-1:0:1;}int main(int H,char**J){if(H<2
|
||||
)return 1;char*p,*
|
||||
P,n;size_t L=0,i
|
||||
,S=0,U=0;FILE*f=
|
||||
fopen(J[1],"r");
|
||||
for(;(n=fgetc(f)
|
||||
)!= EOF ; L+=n==
|
||||
'\n') ;rewind(f);
|
||||
size_t G[L];for(
|
||||
L= 0; fscanf(f,"%"
|
||||
"ms",&p)==1;L++){char
|
||||
s[strlen(p)],*B=s;G[L]=0;for(P=p
|
||||
;*P;P++)for(i=0;i<sizeof(C);i
|
||||
+=2)if(*P==C[ i])*B++=C[i+
|
||||
1];else if(*P==C[i+1
|
||||
])if(B== s||*(B-
|
||||
1)!=*P){S+=X[i/2
|
||||
];goto N;}else B
|
||||
typedef unsigned __int128 u;int X[
|
||||
]= {3,57,1197,25137};void O(u n){if(n>
|
||||
#define P10uI64 10000000000000000000ULL
|
||||
UINT64_MAX) { O ( n /P10uI64);n%=P10uI64;}
|
||||
printf( "%"PRIu64,(uint64_t)n);}int Q(const
|
||||
void* A , const void*B){u a=*(u*)A,b=*(u*)B;
|
||||
return a>b?a<b?-1:
|
||||
0: 1 ;}char C[]={
|
||||
'(', ')','[',']',
|
||||
'{','}','<','>'};
|
||||
int main(int H,
|
||||
char**J){if(H<2)
|
||||
return 1;char*p,
|
||||
*P,n;u L=0,i,S=0,
|
||||
U=0;FILE*f=fopen
|
||||
(J[1],"r");for(;(n
|
||||
=fgetc (f))!=EOF;L+=n
|
||||
=='\n' ) ;rewind(f);u G[L];for(L=0;
|
||||
fscanf(f,"%ms",&p)==1;L++){char s
|
||||
[strlen(p)],*B=s;G[L]=0;for(P=
|
||||
p;*P;P++)for(i=0;i<sizeof(C
|
||||
);i+=2)if(*P==C[i])*B
|
||||
++=C[i+1];else if
|
||||
(* P==C[i+1])if(B
|
||||
==s||*(B-1)!=*P){
|
||||
S+=X[i/2];goto N;
|
||||
|
||||
|
||||
--;if(B!=
|
||||
s){for(B--;B>=s;B
|
||||
--)for(i=0; i<sizeof(
|
||||
C);i+=2)if(*B==C[i+1]){
|
||||
G[L]=G[ L]*5 +i/2+1;break
|
||||
;}U++;}N:free(p) ;}qsort(
|
||||
&G,L,sizeof(size_t),Q);
|
||||
printf("Silver: %lu\n"
|
||||
"Gold: %lu\n",S,G
|
||||
[U/2 ]);}
|
||||
}else B--;
|
||||
if(B!=s){for(B--;B
|
||||
>=s ; B -- )for(i=0;i<
|
||||
sizeof(C);i+=2)if(*B==C[
|
||||
i+ 1 ]){G[L]=G[L]*5+i/2+1;
|
||||
break; } U ++;}N:free(p);}
|
||||
qsort( & G,L,sizeof(u),Q);
|
||||
printf("\xe2\x98\x86");O
|
||||
(S);printf("\n\xe2\x98"
|
||||
"\x85");O(G[U/2]);
|
||||
puts("");}
|
||||
|
|
Loading…
Reference in a new issue