code.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type Equation struct {
  11. result int
  12. numbers []int
  13. }
  14. func readInput(file *os.File) []Equation {
  15. scanner := bufio.NewScanner(file)
  16. var equations []Equation
  17. for scanner.Scan() {
  18. line := scanner.Text()
  19. if line == "" {
  20. break
  21. }
  22. var equation Equation
  23. parts := strings.Split(line, ": ")
  24. if len(parts) != 2 {
  25. log.Fatalf("Bad input: %s", line)
  26. }
  27. var err error
  28. equation.result, err = strconv.Atoi(parts[0])
  29. if err != nil {
  30. log.Fatalf("Problem parsing '%s': %s", parts[0], err)
  31. }
  32. numbers := strings.Split(parts[1], " ")
  33. for _, number := range numbers {
  34. item, err := strconv.Atoi(number)
  35. if err != nil {
  36. log.Fatalf("Problem parsing '%s': %s", number, err)
  37. }
  38. equation.numbers = append(equation.numbers, item)
  39. }
  40. equations = append(equations, equation)
  41. }
  42. return equations
  43. }
  44. func concatenate(a, b int) int {
  45. if a == 0 {
  46. return b
  47. }
  48. result, _ := strconv.Atoi(strconv.Itoa(a) + strconv.Itoa(b))
  49. return result
  50. }
  51. func check(equation Equation, index, result int, conc bool) bool {
  52. if result > equation.result {
  53. return false
  54. }
  55. if index >= len(equation.numbers) {
  56. if result == equation.result {
  57. return true
  58. }
  59. return false
  60. }
  61. resultAdd := check(equation, index+1, result+equation.numbers[index], conc)
  62. resultConc := false
  63. if conc {
  64. resultConc = check(equation, index+1, concatenate(result, equation.numbers[index]), conc)
  65. }
  66. if result == 0 {
  67. result++
  68. }
  69. resultMul := check(equation, index+1, result*equation.numbers[index], conc)
  70. if resultAdd {
  71. return resultAdd
  72. }
  73. if resultConc {
  74. return resultConc
  75. }
  76. return resultMul
  77. }
  78. func parts(equations []Equation) (int, int) {
  79. var part1 int
  80. var toRecheck []Equation
  81. for _, equation := range equations {
  82. if check(equation, 0, 0, false) {
  83. part1 += equation.result
  84. } else {
  85. toRecheck = append(toRecheck, equation)
  86. }
  87. }
  88. part2 := part1
  89. for _, equation := range toRecheck {
  90. if check(equation, 0, 0, true) {
  91. part2 += equation.result
  92. }
  93. }
  94. return part1, part2
  95. }
  96. func main() {
  97. if len(os.Args) < 2 {
  98. log.Fatal("You need to specify a file!")
  99. }
  100. filePath := os.Args[1]
  101. file, err := os.Open(filePath)
  102. if err != nil {
  103. log.Fatalf("Failed to open %s!\n", filePath)
  104. }
  105. equations := readInput(file)
  106. part1, part2 := parts(equations)
  107. fmt.Println("Part1:", part1)
  108. fmt.Println("Part2:", part2)
  109. }