|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "slices" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + |
| 12 | + file "github.com/shaunburdick/advent-of-code-2024/lib" |
| 13 | +) |
| 14 | + |
| 15 | +var input string |
| 16 | + |
| 17 | +func init() { |
| 18 | + // do this in init (not main) so test file has same input |
| 19 | + inputFile, err := file.LoadRelativeFile("input.txt") |
| 20 | + if err != nil { |
| 21 | + log.Println(err) |
| 22 | + } |
| 23 | + |
| 24 | + input = strings.TrimRight(inputFile, "\n") |
| 25 | +} |
| 26 | + |
| 27 | +func main() { |
| 28 | + var part int |
| 29 | + flag.IntVar(&part, "part", 1, "part 1 or 2") |
| 30 | + flag.Parse() |
| 31 | + fmt.Println("Running part", part) |
| 32 | + |
| 33 | + if part == 1 { |
| 34 | + ans := part1(input) |
| 35 | + fmt.Println("Output:", ans) |
| 36 | + } else { |
| 37 | + ans := part2(input) |
| 38 | + fmt.Println("Output:", ans) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func part1(input string) int64 { |
| 43 | + parsed := parseInput(input) |
| 44 | + |
| 45 | + var total int64 |
| 46 | + total = 0 |
| 47 | + |
| 48 | + for _, calibration := range parsed { |
| 49 | + testValue, numbers := ParseCalibration(calibration) |
| 50 | + iter := IterateOperators(numbers[0], numbers[1:]) |
| 51 | + if slices.Contains(iter, testValue) { |
| 52 | + total += testValue |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return total |
| 57 | +} |
| 58 | + |
| 59 | +func part2(input string) int64 { |
| 60 | + parsed := parseInput(input) |
| 61 | + _ = parsed |
| 62 | + |
| 63 | + return 0 |
| 64 | +} |
| 65 | + |
| 66 | +func IterateOperators(carry int64, numbers []int64) (results []int64) { |
| 67 | + addResult := ApplyOperator(Add, carry, numbers[0]) |
| 68 | + mulResult := ApplyOperator(Multiply, carry, numbers[0]) |
| 69 | + |
| 70 | + // base case |
| 71 | + if len(numbers) == 1 { |
| 72 | + results = append(results, addResult, mulResult) |
| 73 | + } else { |
| 74 | + nextAdd := IterateOperators(addResult, numbers[1:]) |
| 75 | + nextMul := IterateOperators(mulResult, numbers[1:]) |
| 76 | + |
| 77 | + results = append(results, nextAdd...) |
| 78 | + results = append(results, nextMul...) |
| 79 | + } |
| 80 | + |
| 81 | + return results |
| 82 | +} |
| 83 | + |
| 84 | +func ParseCalibration(c string) (testValue int64, numbers []int64) { |
| 85 | + calSplit := strings.Split(c, ":") |
| 86 | + tv, tvErr := strconv.Atoi(calSplit[0]) |
| 87 | + if tvErr != nil { |
| 88 | + log.Fatalf("Unable to parse test value: %s", calSplit[0]) |
| 89 | + } |
| 90 | + testValue = int64(tv) |
| 91 | + |
| 92 | + for i, num := range strings.Fields(calSplit[1]) { |
| 93 | + numInt, numErr := strconv.Atoi(num) |
| 94 | + if numErr != nil { |
| 95 | + log.Fatalf("Unable to parse number at position %d: %s", i, num) |
| 96 | + } |
| 97 | + |
| 98 | + numbers = append(numbers, int64(numInt)) |
| 99 | + } |
| 100 | + |
| 101 | + return testValue, numbers |
| 102 | +} |
| 103 | + |
| 104 | +type Operator int |
| 105 | + |
| 106 | +const ( |
| 107 | + Add Operator = iota |
| 108 | + Multiply |
| 109 | +) |
| 110 | + |
| 111 | +func ApplyOperator(op Operator, a int64, b int64) int64 { |
| 112 | + switch op { |
| 113 | + case Add: |
| 114 | + return a + b |
| 115 | + case Multiply: |
| 116 | + return a * b |
| 117 | + default: |
| 118 | + panic("Unknown Operator") |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func ApplyOperators(op Operator, numbers []int64) int64 { |
| 123 | + total := ApplyOperator(op, numbers[0], numbers[1]) |
| 124 | + |
| 125 | + if len(numbers) > 2 { |
| 126 | + for _, number := range numbers[2:] { |
| 127 | + total = ApplyOperator(op, total, number) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return total |
| 132 | +} |
| 133 | + |
| 134 | +func parseInput(input string) (ans []string) { |
| 135 | + return strings.Split(input, "\n") |
| 136 | +} |
0 commit comments