|
| 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 mapKey struct { |
| 13 | + integer int64 |
| 14 | + steps int |
| 15 | +} |
| 16 | + |
| 17 | +func part1(input string) int64 { |
| 18 | + integers, err := parseInput(input) |
| 19 | + if err != nil { |
| 20 | + log.Fatal(err) |
| 21 | + } |
| 22 | + |
| 23 | + count := int64(0) |
| 24 | + seen := make(map[mapKey]int64) |
| 25 | + |
| 26 | + for j := 0; j < len(integers); j++ { |
| 27 | + count += recurse(integers[j], 25, seen) |
| 28 | + } |
| 29 | + |
| 30 | + return count |
| 31 | +} |
| 32 | + |
| 33 | +func part2(input string) int64 { |
| 34 | + integers, err := parseInput(input) |
| 35 | + if err != nil { |
| 36 | + log.Fatal(err) |
| 37 | + } |
| 38 | + |
| 39 | + count := int64(0) |
| 40 | + seen := make(map[mapKey]int64) |
| 41 | + |
| 42 | + for j := 0; j < len(integers); j++ { |
| 43 | + count += recurse(integers[j], 75, seen) |
| 44 | + } |
| 45 | + |
| 46 | + return count |
| 47 | +} |
| 48 | + |
| 49 | +func recurse(current int64, steps int, seen map[mapKey]int64) int64 { |
| 50 | + if steps == 0 { |
| 51 | + return 1 |
| 52 | + } |
| 53 | + |
| 54 | + if count, ok := seen[mapKey{current, steps}]; ok { |
| 55 | + return count |
| 56 | + } |
| 57 | + |
| 58 | + if current == 0 { |
| 59 | + count := recurse(1, steps-1, seen) |
| 60 | + seen[mapKey{current, steps}] = count |
| 61 | + return count |
| 62 | + } |
| 63 | + |
| 64 | + digitCount := countDigits(current) |
| 65 | + if digitCount%2 == 1 { |
| 66 | + count := recurse(current*2024, steps-1, seen) |
| 67 | + seen[mapKey{current, steps}] = count |
| 68 | + return count |
| 69 | + } |
| 70 | + |
| 71 | + multiplier := getMultiplier(digitCount / 2) |
| 72 | + count := recurse(current/multiplier, steps-1, seen) + |
| 73 | + recurse(current%multiplier, steps-1, seen) |
| 74 | + seen[mapKey{current, steps}] = count |
| 75 | + return count |
| 76 | +} |
| 77 | + |
| 78 | +func countDigits(number int64) int { |
| 79 | + count := 0 |
| 80 | + for number != 0 { |
| 81 | + number /= 10 |
| 82 | + count++ |
| 83 | + } |
| 84 | + |
| 85 | + return count |
| 86 | +} |
| 87 | + |
| 88 | +func getMultiplier(digits int) int64 { |
| 89 | + multiplier := int64(1) |
| 90 | + for i := 0; i < digits; i++ { |
| 91 | + multiplier *= 10 |
| 92 | + } |
| 93 | + |
| 94 | + return multiplier |
| 95 | +} |
| 96 | + |
| 97 | +func parseInput(input string) ([]int64, error) { |
| 98 | + var data []int64 |
| 99 | + for _, line := range lib.SplitLines(input) { |
| 100 | + if len(line) == 0 { |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + integers, err := lib.StringSliceToInt64(strings.Split(line, " ")) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + data = append(data, integers...) |
| 109 | + } |
| 110 | + |
| 111 | + return data, nil |
| 112 | +} |
| 113 | + |
| 114 | +func main() { |
| 115 | + name := os.Args[1] |
| 116 | + content, err := lib.LoadFileContent(name) |
| 117 | + if err != nil { |
| 118 | + log.Fatal(err) |
| 119 | + } |
| 120 | + |
| 121 | + fmt.Printf("Part 1: %d\n", part1(content)) |
| 122 | + fmt.Printf("Part 2: %d\n", part2(content)) |
| 123 | +} |
0 commit comments