|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/kfarnung/advent-of-code/2024/lib" |
| 10 | +) |
| 11 | + |
| 12 | +type equation struct { |
| 13 | + testValue int64 |
| 14 | + operands []int64 |
| 15 | +} |
| 16 | + |
| 17 | +func part1(input string) int64 { |
| 18 | + equations, err := parseInput(input) |
| 19 | + if err != nil { |
| 20 | + log.Fatal(err) |
| 21 | + } |
| 22 | + |
| 23 | + sum := int64(0) |
| 24 | + for _, equation := range equations { |
| 25 | + if isValidEquation(0, equation.operands, equation.testValue, false) { |
| 26 | + sum += equation.testValue |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return sum |
| 31 | +} |
| 32 | + |
| 33 | +func part2(input string) int64 { |
| 34 | + equations, err := parseInput(input) |
| 35 | + if err != nil { |
| 36 | + log.Fatal(err) |
| 37 | + } |
| 38 | + |
| 39 | + sum := int64(0) |
| 40 | + for _, equation := range equations { |
| 41 | + if isValidEquation(0, equation.operands, equation.testValue, true) { |
| 42 | + sum += equation.testValue |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return sum |
| 47 | +} |
| 48 | + |
| 49 | +func isValidEquation(current int64, operands []int64, target int64, allowConcat bool) bool { |
| 50 | + if len(operands) == 0 { |
| 51 | + return current == target |
| 52 | + } |
| 53 | + |
| 54 | + operand := operands[0] |
| 55 | + operands = operands[1:] |
| 56 | + |
| 57 | + if isValidEquation(current+operand, operands, target, allowConcat) { |
| 58 | + return true |
| 59 | + } |
| 60 | + |
| 61 | + if allowConcat { |
| 62 | + if isValidEquation(current*getMultiplier(operand)+operand, operands, target, allowConcat) { |
| 63 | + return true |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if current == 0 { |
| 68 | + current = 1 |
| 69 | + } |
| 70 | + |
| 71 | + if isValidEquation(current*operand, operands, target, allowConcat) { |
| 72 | + return true |
| 73 | + } |
| 74 | + |
| 75 | + return false |
| 76 | +} |
| 77 | + |
| 78 | +func getMultiplier(num int64) int64 { |
| 79 | + if num == 0 { |
| 80 | + return 1 |
| 81 | + } |
| 82 | + |
| 83 | + multiplier := int64(1) |
| 84 | + for num > 0 { |
| 85 | + num /= 10 |
| 86 | + multiplier *= 10 |
| 87 | + } |
| 88 | + |
| 89 | + return multiplier |
| 90 | +} |
| 91 | + |
| 92 | +func parseInput(input string) ([]equation, error) { |
| 93 | + var equations []equation |
| 94 | + for _, line := range lib.SplitLines(input) { |
| 95 | + before, after, found := strings.Cut(line, ": ") |
| 96 | + if !found { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + testValue, err := lib.ParseInt[int64](before) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + var operands []int64 |
| 106 | + for _, operandValue := range strings.Split(after, " ") { |
| 107 | + operand, err := lib.ParseInt[int64](operandValue) |
| 108 | + if err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + operands = append(operands, operand) |
| 113 | + } |
| 114 | + |
| 115 | + equations = append(equations, equation{testValue: testValue, operands: operands}) |
| 116 | + } |
| 117 | + |
| 118 | + return equations, nil |
| 119 | +} |
| 120 | + |
| 121 | +func main() { |
| 122 | + name := os.Args[1] |
| 123 | + content, err := lib.LoadFileContent(name) |
| 124 | + if err != nil { |
| 125 | + log.Fatal(err) |
| 126 | + } |
| 127 | + |
| 128 | + fmt.Printf("Part 1: %d\n", part1(content)) |
| 129 | + fmt.Printf("Part 2: %d\n", part2(content)) |
| 130 | +} |
0 commit comments