day14.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. var id int64
  32. var number int64
  33. n, err := fmt.Sscanf(line, "mem[%d] = %d", &id, &number)
  34. if err != nil || n != 2 {
  35. return fmt.Errorf("Error scanning '%s': %s", line, err)
  36. }
  37. number = setBits(number, mask)
  38. mem[id] = number
  39. return nil
  40. }
  41. func readFile(file *os.File) {
  42. scanner := bufio.NewScanner(file)
  43. for scanner.Scan() {
  44. line := scanner.Text()
  45. if line == "" {
  46. break
  47. }
  48. if strings.Contains(line, "mask") {
  49. n, err := fmt.Sscanf(line, "mask = %s\n", &mask)
  50. if err != nil || n != 1 {
  51. log.Fatalf("Error scanning '%s': %s", line, err)
  52. }
  53. continue
  54. }
  55. if err := processLine(line); err != nil {
  56. log.Fatal(err)
  57. }
  58. if err := processLine2(line); err != nil {
  59. log.Fatal(err)
  60. }
  61. }
  62. if err := scanner.Err(); err != nil {
  63. log.Fatalf("Scanner error: %s", err)
  64. }
  65. }
  66. func sum(memory map[int64]int64) int64 {
  67. var sum int64
  68. for _, value := range memory {
  69. sum += value
  70. }
  71. return sum
  72. }
  73. func permuteMask(index int64, masksSoFar []string) []string {
  74. if index < 0 {
  75. return masksSoFar
  76. }
  77. var newMasks []string
  78. for _, mask := range masksSoFar {
  79. if mask[index] != 'X' {
  80. newMasks = append(newMasks, mask)
  81. continue
  82. }
  83. newMask1 := []byte(mask)
  84. newMask1[index] = '1'
  85. newMasks = append(newMasks, string(newMask1))
  86. newMask2 := []byte(mask)
  87. newMask2[index] = '0'
  88. newMasks = append(newMasks, string(newMask2))
  89. }
  90. return permuteMask(index-1, newMasks)
  91. }
  92. func setBitsString(number string) string {
  93. newNumber := []byte(number)
  94. for i := 0; i <= maskMaxIndex; i++ {
  95. switch mask[i] {
  96. case 'X':
  97. newNumber[i] = 'X'
  98. case '1':
  99. newNumber[i] = '1'
  100. case '0':
  101. continue
  102. }
  103. }
  104. return string(newNumber)
  105. }
  106. var mem2 map[int64]int64
  107. func processLine2(line string) error {
  108. var id int64
  109. var number int64
  110. n, err := fmt.Sscanf(line, "mem[%d] = %d", &id, &number)
  111. if err != nil || n != 2 {
  112. return fmt.Errorf("Error scanning '%s': %s", line, err)
  113. }
  114. numberString := fmt.Sprintf("%036b", id)
  115. result := setBitsString(numberString)
  116. masks := permuteMask(maskMaxIndex, []string{result})
  117. for _, currentMask := range masks {
  118. currentID, err := strconv.ParseInt(currentMask, 2, 64)
  119. if err != nil {
  120. return fmt.Errorf("Error parsing timestamp %s: %s", currentMask, err)
  121. }
  122. mem2[currentID] = number
  123. }
  124. return nil
  125. }
  126. func init() {
  127. mem = make(map[int64]int64)
  128. mem2 = make(map[int64]int64)
  129. }
  130. func main() {
  131. if len(os.Args) < 2 {
  132. log.Fatal("You need to specify a file!")
  133. }
  134. filePath := os.Args[1]
  135. file, err := os.Open(filePath)
  136. if err != nil {
  137. log.Fatalf("Failed to open %s!\n", filePath)
  138. }
  139. readFile(file)
  140. if err := file.Close(); err != nil {
  141. log.Fatalf("Failed to close file: %s", err)
  142. }
  143. fmt.Println("Part1:", sum(mem))
  144. fmt.Println("Part2:", sum(mem2))
  145. }