code.go 749 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type Hand struct {
  9. cards string
  10. bid int
  11. }
  12. func readInput(file *os.File) []Hand {
  13. scanner := bufio.NewScanner(file)
  14. var hands []Hand
  15. for scanner.Scan() {
  16. line := scanner.Text()
  17. if line == "" {
  18. break
  19. }
  20. var hand Hand
  21. n, err := fmt.Sscanf(line, "%s %d", &hand.cards, &hand.bid)
  22. if n != 2 || err != nil {
  23. log.Fatalf("Failed to read hand: %s\n%s", line, err)
  24. }
  25. hands = append(hands, hand)
  26. }
  27. return hands
  28. }
  29. func main() {
  30. if len(os.Args) < 2 {
  31. log.Fatal("You need to specify a file!")
  32. }
  33. filePath := os.Args[1]
  34. file, err := os.Open(filePath)
  35. if err != nil {
  36. log.Fatalf("Failed to open %s!\n", filePath)
  37. }
  38. hands := readInput(file)
  39. fmt.Println(hands)
  40. }