code.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type point struct {
  11. x, y int
  12. }
  13. type fold struct {
  14. val int
  15. cat string
  16. }
  17. func readInput(file string) ([]point, []fold) {
  18. content, err := ioutil.ReadFile(file)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. lines := strings.Split(string(content), "\n")
  23. var input []point
  24. var folds []fold
  25. readingPoints := true
  26. for _, line := range lines {
  27. if line == "" {
  28. readingPoints = false
  29. continue
  30. }
  31. if readingPoints {
  32. parts := strings.Split(line, ",")
  33. if len(parts) != 2 {
  34. log.Fatal("Invalid input")
  35. }
  36. x, err := strconv.Atoi(parts[0])
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. y, err := strconv.Atoi(parts[1])
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. input = append(input, point{x, y})
  45. } else {
  46. parts := strings.Split(line, "=")
  47. if len(parts) != 2 {
  48. log.Fatal("Invalid input")
  49. }
  50. val, err := strconv.Atoi(parts[1])
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. if parts[0] == "fold along x" {
  55. folds = append(folds, fold{val, "x"})
  56. } else if parts[0] == "fold along y" {
  57. folds = append(folds, fold{val, "y"})
  58. } else {
  59. log.Fatal("Invalid input")
  60. }
  61. }
  62. }
  63. return input, folds
  64. }
  65. func foldByY(input []point, foldY int) {
  66. for i, p := range input {
  67. if p.y < foldY {
  68. continue
  69. }
  70. newY := p.y % foldY
  71. if newY > 0 {
  72. newY = foldY - newY
  73. }
  74. input[i].y = newY
  75. }
  76. }
  77. func foldByX(input []point, foldX int) {
  78. for i, p := range input {
  79. if p.x > foldX {
  80. input[i].x = p.x%foldX - 1
  81. continue
  82. }
  83. input[i].x = foldX - p.x - 1
  84. }
  85. }
  86. func countPoints(input []point) int {
  87. counted := make(map[point]bool)
  88. count := 0
  89. for _, p := range input {
  90. if counted[p] {
  91. continue
  92. }
  93. counted[p] = true
  94. count++
  95. }
  96. return count
  97. }
  98. func part1(input []point, folds []fold) int {
  99. firstFold := folds[0]
  100. if firstFold.cat == "x" {
  101. foldByX(input, firstFold.val)
  102. } else {
  103. foldByY(input, firstFold.val)
  104. }
  105. return countPoints(input)
  106. }
  107. func main() {
  108. if len(os.Args) < 2 {
  109. log.Fatal("Please provide a file name as argument")
  110. }
  111. input, folds := readInput(os.Args[1])
  112. fmt.Println("Part1:", part1(input, folds))
  113. }