day14.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. var mem map[int64]int64
  11. var mask string
  12. const (
  13. maskMaxIndex = 35
  14. )
  15. func setBits(number int64, mask string) int64 {
  16. for i := 0; i <= maskMaxIndex; i++ {
  17. switch mask[maskMaxIndex-i] {
  18. case 'X':
  19. continue
  20. case '1':
  21. number |= (1 << i)
  22. case '0':
  23. var tempMask int64
  24. tempMask = ^(1 << i)
  25. number &= tempMask
  26. }
  27. }
  28. return number
  29. }
  30. func processLine(line string) error {
  31. if strings.Contains(line, "mask") {
  32. n, err := fmt.Sscanf(line, "mask = %s\n", &mask)
  33. if err != nil || n != 1 {
  34. return fmt.Errorf("Error scanning '%s': %s", line, err)
  35. }
  36. return nil
  37. }
  38. var id int64
  39. var number int64
  40. n, err := fmt.Sscanf(line, "mem[%d] = %d", &id, &number)
  41. if err != nil || n != 2 {
  42. return fmt.Errorf("Error scanning '%s': %s", line, err)
  43. }
  44. number = setBits(number, mask)
  45. mem[id] = number
  46. return nil
  47. }
  48. func readFile(file *os.File) {
  49. scanner := bufio.NewScanner(file)
  50. for scanner.Scan() {
  51. line := scanner.Text()
  52. if line == "" {
  53. break
  54. }
  55. if err := processLine(line); err != nil {
  56. log.Fatal(err)
  57. }
  58. }
  59. if err := scanner.Err(); err != nil {
  60. log.Fatalf("Scanner error: %s", err)
  61. }
  62. }
  63. func sum(memory map[int64]int64) int64 {
  64. var sum int64
  65. for _, value := range memory {
  66. sum += value
  67. }
  68. return sum
  69. }
  70. func permuteMask(index int64, masksSoFar []string) []string {
  71. if index < 0 {
  72. return masksSoFar
  73. }
  74. var newMasks []string
  75. for _, mask := range masksSoFar {
  76. if mask[index] != 'X' {
  77. newMasks = append(newMasks, mask)
  78. continue
  79. }
  80. newMask1 := []byte(mask)
  81. newMask1[index] = '1'
  82. newMasks = append(newMasks, string(newMask1))
  83. newMask2 := []byte(mask)
  84. newMask2[index] = '0'
  85. newMasks = append(newMasks, string(newMask2))
  86. }
  87. return permuteMask(index-1, newMasks)
  88. }
  89. func setBitsString(number string) string {
  90. newNumber := []byte(number)
  91. for i := 0; i <= maskMaxIndex; i++ {
  92. switch mask2[i] {
  93. case 'X':
  94. newNumber[i] = 'X'
  95. case '1':
  96. newNumber[i] = '1'
  97. case '0':
  98. continue
  99. }
  100. }
  101. return string(newNumber)
  102. }
  103. var mem2 map[int64]int64
  104. var mask2 string
  105. func processLine2(line string) error {
  106. if strings.Contains(line, "mask") {
  107. n, err := fmt.Sscanf(line, "mask = %s\n", &mask2)
  108. if err != nil || n != 1 {
  109. return fmt.Errorf("Error scanning '%s': %s", line, err)
  110. }
  111. return nil
  112. }
  113. var id int64
  114. var number int64
  115. n, err := fmt.Sscanf(line, "mem[%d] = %d", &id, &number)
  116. if err != nil || n != 2 {
  117. return fmt.Errorf("Error scanning '%s': %s", line, err)
  118. }
  119. numberString := fmt.Sprintf("%036b", id)
  120. result := setBitsString(numberString)
  121. masks := permuteMask(maskMaxIndex, []string{result})
  122. for _, currentMask := range masks {
  123. currentID, err := strconv.ParseInt(currentMask, 2, 64)
  124. if err != nil {
  125. return fmt.Errorf("Error parsing timestamp %s: %s", currentMask, err)
  126. }
  127. mem2[currentID] = number
  128. }
  129. return nil
  130. }
  131. func readFile2(file *os.File) {
  132. scanner := bufio.NewScanner(file)
  133. for scanner.Scan() {
  134. line := scanner.Text()
  135. if line == "" {
  136. break
  137. }
  138. if err := processLine2(line); err != nil {
  139. log.Fatal(err)
  140. }
  141. }
  142. if err := scanner.Err(); err != nil {
  143. log.Fatalf("Scanner error: %s", err)
  144. }
  145. }
  146. func init() {
  147. mem = make(map[int64]int64)
  148. mem2 = make(map[int64]int64)
  149. }
  150. func main() {
  151. if len(os.Args) < 2 {
  152. log.Fatal("You need to specify a file!")
  153. }
  154. filePath := os.Args[1]
  155. file, err := os.Open(filePath)
  156. if err != nil {
  157. log.Fatalf("Failed to open %s!\n", filePath)
  158. }
  159. readFile(file)
  160. fmt.Println("Part1:", sum(mem))
  161. if err := file.Close(); err != nil {
  162. log.Fatalf("Failed to close file: %s", err)
  163. }
  164. file, err = os.Open(filePath)
  165. if err != nil {
  166. log.Fatalf("Failed to open %s!\n", filePath)
  167. }
  168. readFile2(file)
  169. if err := file.Close(); err != nil {
  170. log.Fatalf("Failed to close file: %s", err)
  171. }
  172. fmt.Println("Part2:", sum(mem2))
  173. }