From dff0a314f2a08fb7e150c7f236b62596891b7db7 Mon Sep 17 00:00:00 2001 From: Scott McKendry <39483124+scottmckendry@users.noreply.github.com> Date: Tue, 28 Nov 2023 16:54:59 +1300 Subject: [PATCH] Add 2022 Day 12 p2 --- 2022/12p1.go | 28 ++++++++++++++++++------ 2022/12p2.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2022/main.go | 1 + 3 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 2022/12p2.go diff --git a/2022/12p1.go b/2022/12p1.go index 4b3af46..69f5929 100644 --- a/2022/12p1.go +++ b/2022/12p1.go @@ -27,7 +27,11 @@ func D12P1() { queue := []nodeCoordinate{} // Append start node to queue - start := findStart(hills) + start := findStart(hills, false) + value, exists := hills[start] + if exists { + value.movesFromStart = 0 + } queue = append(queue, start) for queue != nil { @@ -41,7 +45,7 @@ func D12P1() { break } - neighbours := findNeighbours(&hills, queue[0]) + neighbours := findNeighbours(&hills, queue[0], false) for _, n := range neighbours { value, exists := hills[n] @@ -74,7 +78,6 @@ func parseHills(lines []string) map[nodeCoordinate]node { if string(char) == "S" { char = 'a' start = true - movesFromStart = 0 } if string(char) == "E" { char = 'z' @@ -95,16 +98,23 @@ func parseHills(lines []string) map[nodeCoordinate]node { return hills } -func findStart(hills map[nodeCoordinate]node) nodeCoordinate { +func findStart(hills map[nodeCoordinate]node, reverse bool) nodeCoordinate { for coordinate, hill := range hills { - if hill.start { + if hill.start && !reverse { + return coordinate + } + if hill.end && reverse { return coordinate } } return nodeCoordinate{} } -func findNeighbours(hills *map[nodeCoordinate]node, coordinate nodeCoordinate) []nodeCoordinate { +func findNeighbours( + hills *map[nodeCoordinate]node, + coordinate nodeCoordinate, + reverse bool, +) []nodeCoordinate { neighbours := []nodeCoordinate{} posibleNeighbours := []nodeCoordinate{ {coordinate.x + 1, coordinate.y}, @@ -117,7 +127,11 @@ func findNeighbours(hills *map[nodeCoordinate]node, coordinate nodeCoordinate) [ for _, possibleNeighbour := range posibleNeighbours { value, exists := (*hills)[possibleNeighbour] - if exists && value.value-currentHillValue > 1 { + if exists && value.value-currentHillValue > 1 && !reverse { + continue + } + + if exists && currentHillValue-value.value > 1 && reverse { continue } diff --git a/2022/12p2.go b/2022/12p2.go new file mode 100644 index 0000000..4634ae3 --- /dev/null +++ b/2022/12p2.go @@ -0,0 +1,60 @@ +package main + +import ( + "fmt" + + "aoc2022/utils" +) + +func D12P2() { + lines := utils.ReadLines("./inputs/12.txt") + hills := parseHills(lines) + + queue := []nodeCoordinate{} + + end := findStart(hills, true) + value, exists := hills[end] + if exists { + value.movesFromStart = 0 + } + hills[end] = value + queue = append(queue, end) + + bestRoute := int(^uint(0) >> 1) + + for len(queue) > 0 { + if hills[queue[0]].visited { + queue = queue[1:] + continue + } + + if hills[queue[0]].letter == "a" { + if hills[queue[0]].movesFromStart < bestRoute { + bestRoute = hills[queue[0]].movesFromStart + } + } + + neighbours := findNeighbours(&hills, queue[0], true) + + for _, n := range neighbours { + value, exists := hills[n] + + if exists { + value.movesFromStart = updateMovesFromStart(value, hills[queue[0]]) + hills[n] = value + } + + queue = append(queue, n) + } + + value, exists := hills[queue[0]] + if exists { + value.visited = true + hills[queue[0]] = value + } + queue = queue[1:] + + } + + fmt.Printf("Best route: %d\n", bestRoute) +} diff --git a/2022/main.go b/2022/main.go index 2279fd1..14242c4 100644 --- a/2022/main.go +++ b/2022/main.go @@ -34,6 +34,7 @@ var solutions = map[string]func(){ "11P1:Monkey in the Middle": D11P1, "11P2:Monkey in the Middle": D11P2, "12P1:Hill Climbing Algorithm": D12P1, + "12P2:Hill Climbing Algorithm": D12P2, } func main() {