[alt.sources] Here's an Acrostic Maker in awk!

manny@wet.UUCP (manny juan) (10/12/90)

#
# ACROSTIC MAKER
#
# by Manny Juan
#    61 Oakmont Drive
#    Daly City, CA 94015
#
#    email: manny@wet.UUCP
#
#    October 10, 1990
#
# Instructions:
#
#  Edit a file similar to the example below (each card starts at column 1).
#  You may cut the segment below to create SAMPLE.PZL after deleting the
#  first two characters of each card.
#
# 12345678901234567890...
#---- SAMPLE.PZL --- cut here
# quote without reagan there would have been
# quote no gorbachev
# quote
# word enough : sufficient
# word throw : cast
# word broad : wide
# word watch : timepiece
# word elevate : what otis machines do
# word avenue : street
# word neighbor : person next door
#---- end of sample.pzl
#
#  Enter the quotation using keyword "quote" WITHOUT punctuations
#  using as many "quote" cards as needed.  Supply final "quote" by
#  itself to terminate the quotation.
#
#  Cards with keyword "word" define the clue words.  The definition
#  portion may be delayed until the last minute when all letters in
#  the quotation have been used.  Start with a few clue words at the
#  beginning and execute the program by typing:
#
#     awk acrostic sample.pzl
#
#  Repeat the editing and checking as long as more letters remain in
#  the letter pool.  During these runs, output will be displayed on the
#  screen.
#
#  When all letters in the original quote have been used, re-edit the
#  puzzle file and supply the word definitions by appending a colon (:)
#  and the definition to each clue word.
#
#  Print the puzzle by typing:
#
#     awk acrostic trace=off sample.pzl >output.dvc
#
#  Note: The printer width is assumed to be 132.  To adjust, for example
#  to 80, type:
#
#     awk acrostic ls=80 trace=off sample.pzl >output.dvc
#
BEGIN {
  if(ARGC<2) {
    err=1
    print("Usage: awk acrostic [ls=nn] [trace=off] input.pzl [>output]")
    exit
  }
  ls=132 # default line size
  uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  lowers = "abcdefghijklmnopqrstuvwxyz"
  alf="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  wn=0 #word counter
  qstr=""
  qlst=""
  err=1 # clear this when final "quote" is read
  }
#
{
  for(i=1;i<=26;i++){
    gsub(substr(lowers,i,1),substr(uppers,i,1))
  }
}
$1=="QUOTE" {
  for(i=2;i<=NF;i++) {
    qstr=qstr " " $i
    qlst=qlst $i
  }
}
$1=="QUOTE" && $2=="" {
  for(i=1;i<=length(qlst);i++) {
    l=substr(qlst,i,1)
    qcount[l]++
    qllist[l]=qllist[l] " " i
  }
  err=0 # to clear err set in BEGIN

  if(trace!="off")
    print("pool=",ltrpool())
}
$1=="WORD" {
  wn++
  word=$2
  wdlist[wn]=$2
  wcount[wn]=length(word)
  wclues[wn]=$0
  if(trace!="off")
    print("\nword=", word)
  for(i=1;i<=length(word);i++){
    c=substr(word,i,1)
    if(qcount[c]==0){
      print"cannot find word " word " in pool"
      exit
    }
    else {
      split(qllist[c], ql, " ")
      wllist[wn]=wllist[wn] " " ql[qcount[c]]
      # strip off last entry
      qlwork=ql[qcount[c]]
      #update solution string
      qsol[qlwork]=substr(alf,wn,1)
      #remove from list
      match(qllist[c], qlwork)
      qllist[c]=substr(qllist[c],1,RSTART-1)
      qcount[c]--
    }
  }
  if(trace!="off")
    print("pool=",ltrpool())
}
END {
  poolsize=0
  for(i=1;i<=26;i++){
    l=substr(alf,i,1)
    if(qcount[l]>0){
      poolsize=poolsize+qcount[l]
    }
  }
  if(err) {
    exit
  }
  if(poolsize!=0) {
    print("\nletters still remain in the pool")
    exit
  }

#print puzzle
  mid=int(ls/2)
  print("CLUES:")
  for(i=1;i<=wn;i++){
    # locate ":" and get clue from string following
    match(wclues[i],":")
    if(RSTART==0)
      clue=wdlist[i]
    else
      clue=substr(wclues[i],RSTART+1)
    printf("\n\n%1s. %-" mid "s",substr(alf,i,1), clue)
    for(j=1;j<=wcount[i];j++) {
      printf("%-4s","___ ")
    }
    printf("\n  %" mid "s"," ")
    split(wllist[i],w)
    for(j=1;j<=wcount[i];j++) {
      printf("%4d",w[j])
    }
  }
  printf("\n")

#print matrix
  cols=int(ls/5)
  rows=int(length(qlst)/cols)+1
  printf("\n\n")
# get beyond initial blank in qstr
  qstr=substr(qstr,2)
  n=0
  for(r=1;r<=rows;r++){
    s1=""
    s2=""
    s3=""
    for(i=1;i<=cols;i++){
      s1=s1 sprintf("%-5s","+----")
    }
    s1=s1 "+"
    print s1
    for(i=1;i<=cols;i++){
      ch=substr(qstr,1,1)
      qstr=substr(qstr,2) " "
      if(ch==" ") {
        s2=s2 "|@@@@"
        s3=s3 "|@@@@"
      }
      else {
        n++
        s2=s2 sprintf("|%s%-3d",qsol[n],n)
        s3=s3 "|    "
      }
    }
    s2=s2 "|"
    s3=s3 "|"
    print s2
    print s3
    print s3
  }
  print s1
}

function rptch(c,n,t) { # return string of n c's
  while(n-- >0)
    t=t c
  return t
}

function ltrpool(x,pool) {
  pool=""
  for(i=1;i<=26;i++){
    l=substr(alf,i,1)
    if(qcount[l]>0){
      pool=pool rptch(l,qcount[l])
    }
  }
  return(pool)
}