github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/chem/main.go (about)

     1  // Spelling with Chemistry
     2  // see:
     3  //   https://www.reddit.com/r/dailyprogrammer/comments/5seexn/20170206_challenge_302_easy_spelling_with/
     4  //   https://www.reddit.com/r/programming/comments/6bgxia/using_python_to_find_the_longest_word_spellable/
     5  //
     6  // This demonstrates using a generated parser from trie.
     7  //
     8  
     9  package main
    10  
    11  import (
    12  	"bufio"
    13  	"fmt"
    14  	"io"
    15  	"os"
    16  	"time"
    17  
    18  	"github.com/egonelbre/exp/chem/element"
    19  )
    20  
    21  func main() {
    22  	var matcher element.Matcher
    23  	var src io.Reader
    24  	src = os.Stdin
    25  	if len(os.Args) > 1 {
    26  		src, _ = os.Open(os.Args[1])
    27  	}
    28  	scanner := bufio.NewScanner(src)
    29  	start := time.Now()
    30  	total := 0
    31  	for scanner.Scan() {
    32  		matcher.Init(scanner.Text())
    33  		matcher.Run()
    34  		total++
    35  	}
    36  	stop := time.Now()
    37  	fmt.Println(stop.Sub(start) / time.Duration(total))
    38  }