day9.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "strconv"
  8. "time"
  9. )
  10. func main() {
  11. example := flag.Bool("example", false, "example or input")
  12. flag.Parse()
  13. filename := "input.txt"
  14. if *example {
  15. filename = "example.txt"
  16. }
  17. file, err := os.Open(filename)
  18. if err != nil {
  19. fmt.Println(err)
  20. return
  21. }
  22. defer file.Close()
  23. scanner := bufio.NewScanner(file)
  24. scanner.Scan()
  25. diskMap := scanner.Text()
  26. // part 1
  27. blocks := parseDiskMap(diskMap)
  28. start := time.Now()
  29. compactDisk(blocks)
  30. checksum := calculateChecksum(blocks)
  31. end1 := time.Since(start)
  32. // part 2
  33. blocks = parseDiskMap(diskMap)
  34. start = time.Now()
  35. compactDiskWholeFiles(blocks)
  36. checksum2 := calculateChecksum(blocks)
  37. end2 := time.Since(start)
  38. fmt.Println("Part 1 CheckSum:", checksum, "with time:", end1)
  39. fmt.Println("Part 2 CheckSum:", checksum2, "with time:", end2)
  40. }
  41. func parseDiskMap(diskMap string) []rune {
  42. var blocks []rune
  43. id := 0
  44. for i, c := range diskMap {
  45. length, _ := strconv.Atoi(string(c))
  46. isFile := i%2 == 0
  47. for j := 0; j < length; j++ {
  48. if isFile {
  49. blocks = append(blocks, rune('0'+id))
  50. } else {
  51. blocks = append(blocks, '.')
  52. }
  53. }
  54. if isFile {
  55. id++
  56. }
  57. }
  58. return blocks
  59. }
  60. func compactDisk(blocks []rune) {
  61. n := len(blocks)
  62. j := 1
  63. for i := n - 1; i >= 0; i-- {
  64. if blocks[i] != '.' {
  65. for j < i {
  66. if blocks[j] == '.' {
  67. blocks[j] = blocks[i]
  68. blocks[i] = '.'
  69. break
  70. }
  71. j++
  72. }
  73. }
  74. }
  75. }
  76. func compactDiskWholeFiles(blocks []rune) {
  77. whereFiles := make(map[int][2]int)
  78. maxID := 0
  79. ind := 0
  80. for ind < len(blocks)-1 {
  81. if blocks[ind] != '.' {
  82. id := int(blocks[ind] - '0')
  83. start := ind
  84. for ind < len(blocks) && int(blocks[ind]-'0') == id {
  85. ind++
  86. // if ind == len(blocks)-1 {
  87. // break
  88. // }
  89. }
  90. end := ind
  91. whereFiles[id] = [2]int{start, end}
  92. if id > maxID {
  93. maxID = id
  94. }
  95. continue
  96. }
  97. ind++
  98. }
  99. for id := maxID; id >= 0; id-- {
  100. size, _ := whereFiles[id]
  101. start, end := size[0], size[1]
  102. fileSize := end - start
  103. freeStart, freeSize := -1, 0
  104. for i := 0; i < start; i++ {
  105. if blocks[i] == '.' {
  106. if freeStart == -1 {
  107. freeStart = i
  108. }
  109. freeSize++
  110. if freeSize >= fileSize {
  111. for j := 0; j < fileSize; j++ {
  112. blocks[freeStart+j] = rune('0' + id)
  113. blocks[start+j] = '.'
  114. }
  115. maxID--
  116. break
  117. }
  118. } else {
  119. freeStart, freeSize = -1, 0
  120. }
  121. }
  122. }
  123. }
  124. func calculateChecksum(blocks []rune) int {
  125. checksum := 0
  126. for i, block := range blocks {
  127. if block != '.' {
  128. id := int(block - '0')
  129. checksum += i * id
  130. }
  131. }
  132. return checksum
  133. }