|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/fxnn/adventofcode2024/util" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + ADD = '+' |
| 14 | + MUL = '*' |
| 15 | + CON = '|' |
| 16 | +) |
| 17 | + |
| 18 | +var allOperators = []byte{ADD, MUL, CON} |
| 19 | + |
| 20 | +func calculate(operands []int, operators []byte) int { |
| 21 | + var result = operands[0] |
| 22 | + for i := 0; i < len(operators); i++ { |
| 23 | + if operators[i] == ADD { |
| 24 | + result += operands[i+1] |
| 25 | + } else if operators[i] == MUL { |
| 26 | + result *= operands[i+1] |
| 27 | + } else { |
| 28 | + result = util.Atoi(util.Itoa(result) + util.Itoa(operands[i+1])) |
| 29 | + } |
| 30 | + } |
| 31 | + return result |
| 32 | +} |
| 33 | + |
| 34 | +func formula(operands []int, operators []byte) string { |
| 35 | + var s = util.Itoa(operands[0]) |
| 36 | + for i := 0; i < len(operators); i++ { |
| 37 | + s += fmt.Sprintf(" %s %d", string(operators[i]), operands[i+1]) |
| 38 | + } |
| 39 | + |
| 40 | + return s |
| 41 | +} |
| 42 | + |
| 43 | +func modifyOperators(operators []byte, index int, newOperator byte) []byte { |
| 44 | + // we need to copy, because slices are, although passed by value, only shallow copies |
| 45 | + // referring to the same backing array |
| 46 | + var result = make([]byte, len(operators)) |
| 47 | + copy(result[:], operators[:]) |
| 48 | + result[index] = newOperator |
| 49 | + return result |
| 50 | +} |
| 51 | + |
| 52 | +func isOperatorsExistStep(testResult int, operands []int, operators []byte, fixedOperators int) bool { |
| 53 | + if fixedOperators >= len(operators) { |
| 54 | + var match = calculate(operands, operators) == testResult |
| 55 | + if match { |
| 56 | + fmt.Printf("%d: %s\n", testResult, formula(operands, operators)) |
| 57 | + } |
| 58 | + return match |
| 59 | + } |
| 60 | + |
| 61 | + for _, o := range allOperators { |
| 62 | + var newOps = modifyOperators(operators, fixedOperators, o) |
| 63 | + if isOperatorsExistStep(testResult, operands, newOps, fixedOperators+1) { |
| 64 | + return true |
| 65 | + } |
| 66 | + } |
| 67 | + return false |
| 68 | +} |
| 69 | + |
| 70 | +func makeOperators(count int) []byte { |
| 71 | + var operators = make([]byte, count) |
| 72 | + for i := 0; i < count; i++ { |
| 73 | + operators[i] = ADD |
| 74 | + } |
| 75 | + return operators |
| 76 | +} |
| 77 | + |
| 78 | +func isOperatorsExist(testResult int, operands []int) bool { |
| 79 | + var operators = makeOperators(len(operands) - 1) |
| 80 | + var fixedOperators = 0 |
| 81 | + return isOperatorsExistStep(testResult, operands, operators, fixedOperators) |
| 82 | +} |
| 83 | + |
| 84 | +func main() { |
| 85 | + scanner := bufio.NewScanner(os.Stdin) |
| 86 | + scanner.Split(bufio.ScanLines) |
| 87 | + |
| 88 | + var total = 0 |
| 89 | + for scanner.Scan() { |
| 90 | + var line = scanner.Text() |
| 91 | + var parts = strings.SplitN(line, ": ", 2) |
| 92 | + var testResult = util.Atoi(parts[0]) |
| 93 | + var operands = util.AtoiList(strings.Split(parts[1], " ")) |
| 94 | + if isOperatorsExist(testResult, operands) { |
| 95 | + total += testResult |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fmt.Printf("total calibration result: %d\n", total) |
| 100 | +} |
0 commit comments