day14.go 2.9 KB

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