This commit is contained in:
sejo 2021-12-09 23:30:04 -06:00
parent c8fcc5cd13
commit 4e0a38990c
3 changed files with 99 additions and 0 deletions

35
10/10.awk Normal file
View File

@ -0,0 +1,35 @@
function push( c ){
sp++
stack[sp] = c
}
function pop(){
sp--
return stack[sp+1]
}
function peek(){
return stack[sp]
}
BEGIN{
FS = ""
sp = 0
}
{
for(i=1;i<=NF;i++){
t = peek()
if($i=="(" || $i=="[" || $i=="{" || $i=="<"){
push($i)
}
else{
if(i<NF && (($i==")"&&t!="(") || ($i=="]" && t!="[") || ($i=="}" && t!="{") || ($i==">" && t!="<" ))){
# print NR, "corrupted", $i, i
score += ($i==")") ? 3 : ($i=="]") ? 57 : ($i=="}") ? 1197 : 25137
break
}
else pop()
}
}
delete stack
}
END{
print score
}

50
10/10_2.awk Normal file
View File

@ -0,0 +1,50 @@
function push( c ){
sp++
stack[sp] = c
}
function pop(){
sp--
return stack[sp+1]
}
function peek(){
return stack[sp]
}
BEGIN{
FS = ""
sp = 0
scorep = 1
}
{
corrupted = 0
for(i=1;i<=NF;i++){
t = peek()
if($i=="(" || $i=="[" || $i=="{" || $i=="<"){
push($i)
}
else{
if(i<NF && (($i==")"&&t!="(") || ($i=="]" && t!="[") || ($i=="}" && t!="{") || ($i==">" && t!="<" ))){
# print NR, "corrupted", $i, i
corrupted = 1
break
}
else pop()
}
}
if(!corrupted){
score = 0
for(i=sp;i>0;i--){
score *= 5
v = stack[i]
score += (v=="(") ? 1 : v=="[" ? 2 : v=="{" ? 3 : 4
}
scores[scorep++] = score
# print score
}
delete stack
sp = 0
}
END{
asort(scores)
# print length(scores), scorep
print scores[ scorep/2 ]
}

14
10/Makefile Normal file
View File

@ -0,0 +1,14 @@
1t:
awk -f 10.awk test
1i:
awk -f 10.awk input
2t:
awk -f 10_2.awk test
2i:
awk -f 10_2.awk input
paste:
xclip -sel c -o > test