code.go 889 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. fishStrings := strings.Split(string(content), ",")
  16. var input []int
  17. for _, fishString := range fishStrings {
  18. if fishString == "" {
  19. continue
  20. }
  21. if number, err := strconv.Atoi(fishString); err == nil {
  22. input = append(input, number)
  23. } else {
  24. log.Fatal(err)
  25. }
  26. }
  27. return input
  28. }
  29. func part1(fish []int, days int) int {
  30. for d := 0; d < days; d++ {
  31. max := len(fish)
  32. for i := 0; i < max; i++ {
  33. fish[i]--
  34. if fish[i] < 0 {
  35. fish[i] = 6
  36. fish = append(fish, 8)
  37. }
  38. }
  39. }
  40. return len(fish)
  41. }
  42. func main() {
  43. if len(os.Args) < 2 {
  44. log.Fatal("Please provide a file name as argument")
  45. }
  46. input := readInput(os.Args[1])
  47. fmt.Println("Part 1:", part1(input, 80))
  48. }