Compare commits

...

2 Commits

Author SHA1 Message Date
sejo e64178d263 stats for day 10 2021-12-09 23:30:39 -06:00
sejo 4e0a38990c day 10! 2021-12-09 23:30:04 -06:00
4 changed files with 101 additions and 1 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

View File

@ -16,7 +16,7 @@ to run a program, e.g. `01.awk`:
awk -f 01.awk inputfile
```
if using gawk, you should be able to use compatibility/traditional mode (except for day 03, and 09_2)
if using gawk, you should be able to use compatibility/traditional mode (except for day 03, and 09)
```
awk -c -f 01.awk inputfile
@ -27,6 +27,7 @@ awk -c -f 01.awk inputfile
```
-------Part 1-------- -------Part 2--------
Day Time Rank Score Time Rank Score
10 00:19:04 4202 0 00:28:32 3347 0
9 00:12:52 2598 0 00:55:08 4504 0
8 00:06:26 580 0 01:02:03 2196 0
7 00:06:50 2657 0 00:12:29 2665 0