code.go 797 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. )
  9. func readInput(file *os.File) ([]int, int) {
  10. scanner := bufio.NewScanner(file)
  11. numbers := []int{0}
  12. index := 0
  13. max := 0
  14. for scanner.Scan() {
  15. line := scanner.Text()
  16. if line == "" {
  17. if numbers[index] > max {
  18. max = numbers[index]
  19. }
  20. index++
  21. numbers = append(numbers, 0)
  22. continue
  23. }
  24. if number, err := strconv.Atoi(line); err == nil {
  25. numbers[index] += number
  26. } else {
  27. log.Fatal("Numbers: ", err)
  28. }
  29. }
  30. return numbers, max
  31. }
  32. func main() {
  33. if len(os.Args) < 2 {
  34. log.Fatal("You need to specify a file!")
  35. }
  36. filePath := os.Args[1]
  37. file, err := os.Open(filePath)
  38. if err != nil {
  39. log.Fatalf("Failed to open %s!\n", filePath)
  40. }
  41. _, max := readInput(file)
  42. fmt.Println("Part1:", max)
  43. }