Guest User

go submission

a guest
Dec 11th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | Source Code | 0 0
  1.  
  2.  
  3. package main
  4.  
  5. import (
  6. "bufio"
  7. "fmt"
  8. "os"
  9. "time"
  10. )
  11.  
  12. func main() {
  13. // contents, err := LoadNewlineSeparatedStrings("testinput")
  14. start := time.Now()
  15. contents, err := LoadNewlineSeparatedStrings("input")
  16. loadDone := time.Since(start)
  17. if err != nil {
  18. panic(err)
  19. }
  20. PartOne(contents)
  21. p1Done := time.Since(start)
  22. PartTwo(contents)
  23. p2Done := time.Since(start)
  24. fmt.Printf("finished in %v\t(load file: %v\tp1: %v\tp2: %v)\n", time.Since(start), loadDone.Sub(start), p1Done.Sub(loadDone), p2Done.Sub(p1Done))
  25.  
  26. }
  27.  
  28. type node struct {
  29. level int
  30. neighbors []*node
  31. }
  32.  
  33. func parseContents(contents []string) []*node {
  34. startNodes := make([]*node, 0)
  35. nodeMatrix := make([][]*node, 0)
  36. for _, row := range contents {
  37. nodes := make([]*node, 0)
  38. for _, col := range row {
  39. level := int(col) - 48
  40. nodes = append(nodes, &node{
  41. level: level,
  42. neighbors: make([]*node, 0),
  43. })
  44. }
  45. nodeMatrix = append(nodeMatrix, nodes)
  46. }
  47.  
  48. for i := range nodeMatrix {
  49. for j := range nodeMatrix[i] {
  50. n := nodeMatrix[i][j]
  51. if n.level == 0 {
  52. startNodes = append(startNodes, n)
  53. }
  54. if i > 0 && nodeMatrix[i-1][j].level == n.level+1 {
  55. n.neighbors = append(n.neighbors, nodeMatrix[i-1][j])
  56. }
  57. if i < len(nodeMatrix)-1 && nodeMatrix[i+1][j].level == n.level+1 {
  58. n.neighbors = append(n.neighbors, nodeMatrix[i+1][j])
  59. }
  60. if j > 0 && nodeMatrix[i][j-1].level == n.level+1 {
  61. n.neighbors = append(n.neighbors, nodeMatrix[i][j-1])
  62. }
  63. if j < len(nodeMatrix)-1 && nodeMatrix[i][j+1].level == n.level+1 {
  64. n.neighbors = append(n.neighbors, nodeMatrix[i][j+1])
  65. }
  66. }
  67. }
  68. return startNodes
  69. }
  70.  
  71. func PartOne(contents []string) {
  72. startingPoints := parseContents(contents)
  73. trailHeads := 0
  74. for _, n := range startingPoints {
  75. trailHeads += len(countTrailheads(n))
  76. }
  77. fmt.Printf("total trailheads: %d\n", trailHeads)
  78. }
  79.  
  80. func countTrailheads(n *node) map[*node]int {
  81. toVisit := make([]*node, 0)
  82. toVisit = append(toVisit, n.neighbors...)
  83. trailHeads := make(map[*node]int)
  84. for len(toVisit) > 0 {
  85. head := toVisit[0]
  86. toVisit = toVisit[1:]
  87. if head.level == 9 {
  88. if _, exists := trailHeads[head]; !exists {
  89. trailHeads[head] = 0
  90. }
  91. trailHeads[head] += 1
  92. } else {
  93. toVisit = append(toVisit, head.neighbors...)
  94. }
  95. }
  96. return trailHeads
  97. }
  98.  
  99. func PartTwo(contents []string) {
  100. startingPoints := parseContents(contents)
  101. rating := 0
  102. for _, n := range startingPoints {
  103. trailHeadRatings := countTrailheads(n)
  104. for _, v := range trailHeadRatings {
  105. rating += v
  106. }
  107. }
  108. fmt.Printf("total trailhead ratings: %d\n", rating)
  109.  
  110. }
  111.  
  112. func LoadNewlineSeparatedStrings(path string) ([]string, error) {
  113. file, err := os.OpenFile(path, os.O_RDONLY, 0644)
  114. if err != nil {
  115. return nil, fmt.Errorf("load newline separated strings: %w", err)
  116. }
  117. defer file.Close()
  118.  
  119. rows := make([]string, 0)
  120. scanner := bufio.NewScanner(file)
  121. for scanner.Scan() {
  122. rows = append(rows, scanner.Text())
  123. }
  124. if err := scanner.Err(); err != nil {
  125. return nil, fmt.Errorf("load newline separated strings: %w", err)
  126. }
  127. return rows, nil
  128. }
Advertisement
Add Comment
Please, Sign In to add comment