Skip to content

Commit

Permalink
Add 2022 Day 12 p2
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Nov 28, 2023
1 parent 309f837 commit dff0a31
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
28 changes: 21 additions & 7 deletions 2022/12p1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand Down Expand Up @@ -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'
Expand All @@ -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},
Expand All @@ -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
}

Expand Down
60 changes: 60 additions & 0 deletions 2022/12p2.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions 2022/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit dff0a31

Please sign in to comment.