|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/kfarnung/advent-of-code/2024/lib" |
| 9 | +) |
| 10 | + |
| 11 | +func part1(input string) int64 { |
| 12 | + topoMap, trailHeads := parseMap(input) |
| 13 | + sum := int64(0) |
| 14 | + for _, trailHead := range trailHeads { |
| 15 | + terminalPositions := make(map[lib.Point2D]bool) |
| 16 | + findTerminalPositions(topoMap, trailHead, terminalPositions) |
| 17 | + |
| 18 | + sum += int64(len(terminalPositions)) |
| 19 | + } |
| 20 | + |
| 21 | + return sum |
| 22 | +} |
| 23 | + |
| 24 | +func part2(input string) int64 { |
| 25 | + topoMap, trailHeads := parseMap(input) |
| 26 | + sum := int64(0) |
| 27 | + for _, trailHead := range trailHeads { |
| 28 | + terminalPositions := make(map[lib.Point2D]bool) |
| 29 | + sum += findTerminalPositions(topoMap, trailHead, terminalPositions) |
| 30 | + } |
| 31 | + |
| 32 | + return sum |
| 33 | +} |
| 34 | + |
| 35 | +func findTerminalPositions(topoMap [][]rune, position lib.Point2D, terminalPositions map[lib.Point2D]bool) int64 { |
| 36 | + current := topoMap[position.X][position.Y] |
| 37 | + fmt.Printf("current: %v %v\n", current, position) |
| 38 | + if current == '9' { |
| 39 | + terminalPositions[position] = true |
| 40 | + return 1 |
| 41 | + } |
| 42 | + |
| 43 | + count := int64(0) |
| 44 | + target := current + 1 |
| 45 | + |
| 46 | + if position.X > 0 && topoMap[position.X-1][position.Y] == target { |
| 47 | + count += findTerminalPositions(topoMap, lib.Point2D{X: position.X - 1, Y: position.Y}, terminalPositions) |
| 48 | + } |
| 49 | + |
| 50 | + if position.X < int64(len(topoMap)-1) && topoMap[position.X+1][position.Y] == target { |
| 51 | + count += findTerminalPositions(topoMap, lib.Point2D{X: position.X + 1, Y: position.Y}, terminalPositions) |
| 52 | + } |
| 53 | + |
| 54 | + if position.Y > 0 && topoMap[position.X][position.Y-1] == target { |
| 55 | + count += findTerminalPositions(topoMap, lib.Point2D{X: position.X, Y: position.Y - 1}, terminalPositions) |
| 56 | + } |
| 57 | + |
| 58 | + if position.Y < int64(len(topoMap[0])-1) && topoMap[position.X][position.Y+1] == target { |
| 59 | + count += findTerminalPositions(topoMap, lib.Point2D{X: position.X, Y: position.Y + 1}, terminalPositions) |
| 60 | + } |
| 61 | + |
| 62 | + return count |
| 63 | +} |
| 64 | + |
| 65 | +func parseMap(input string) ([][]rune, []lib.Point2D) { |
| 66 | + var topoMap [][]rune |
| 67 | + var trailHeads []lib.Point2D |
| 68 | + for i, line := range lib.SplitLines(input) { |
| 69 | + if len(line) == 0 { |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + var row []rune |
| 74 | + for j, char := range line { |
| 75 | + row = append(row, char) |
| 76 | + |
| 77 | + if char == '0' { |
| 78 | + trailHeads = append(trailHeads, lib.Point2D{X: int64(i), Y: int64(j)}) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + topoMap = append(topoMap, row) |
| 83 | + } |
| 84 | + |
| 85 | + return topoMap, trailHeads |
| 86 | +} |
| 87 | + |
| 88 | +func main() { |
| 89 | + name := os.Args[1] |
| 90 | + content, err := lib.LoadFileContent(name) |
| 91 | + if err != nil { |
| 92 | + log.Fatal(err) |
| 93 | + } |
| 94 | + |
| 95 | + fmt.Printf("Part 1: %d\n", part1(content)) |
| 96 | + fmt.Printf("Part 2: %d\n", part2(content)) |
| 97 | +} |
0 commit comments