code.go 781 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. func readInput(file string) []int {
  11. content, err := ioutil.ReadFile(file)
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. lines := strings.Split(string(content), "\n")
  16. var input []int
  17. for _, line := range lines {
  18. if line == "" {
  19. continue
  20. }
  21. if number, err := strconv.Atoi(line); err == nil {
  22. input = append(input, number)
  23. } else {
  24. log.Fatal(err)
  25. }
  26. }
  27. return input
  28. }
  29. func part1(input []int) int {
  30. increase := 0
  31. for i := 1; i < len(input); i++ {
  32. if input[i-1] < input[i] {
  33. increase++
  34. }
  35. }
  36. return increase
  37. }
  38. func main() {
  39. if len(os.Args) < 2 {
  40. log.Fatal("Please provide a file name as argument")
  41. }
  42. input := readInput(os.Args[1])
  43. fmt.Println("Part 1:", part1(input))
  44. }