mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 12:45:10 +02:00
replaced 1 << 31 - 1 with math.MaxInt32
This commit is contained in:
+2
-3
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -18,9 +19,7 @@ func main() {
|
||||
coordsMap1 := makeCoordinatesMap(split1)
|
||||
coordsMap2 := makeCoordinatesMap(split2)
|
||||
|
||||
// the highest safe int32... https://golang.org/pkg/math/
|
||||
// start this at the highest possible value so it'll be easily overwriteable
|
||||
minDist := 1<<31 - 1
|
||||
minDist := math.MaxInt32
|
||||
|
||||
// iterate over all keys & values in coordsMap1
|
||||
// if that is also in the other map, check if it has a better manhattan dist
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -18,7 +19,7 @@ func main() {
|
||||
coordsMap1 := makeCoordinatesMap(split1)
|
||||
coordsMap2 := makeCoordinatesMap(split2)
|
||||
|
||||
lowestSumOfDistances := 1<<31 - 1
|
||||
lowestSumOfDistances := math.MaxInt32
|
||||
|
||||
// iterate over all keys & values in coordsMap1
|
||||
for key, value1 := range coordsMap1 {
|
||||
|
||||
+2
-1
@@ -12,6 +12,7 @@ import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -398,7 +399,7 @@ removeColsLeft:
|
||||
|
||||
// findShortestLength is a (fairly) standard brute force searching algorithm that will return the shortest path from origin to the O2 tank (cell with a 2)
|
||||
func findShortestLength(grid [][]int, startX, startY int) int {
|
||||
shortestLength := util.MaxSafeInt
|
||||
shortestLength := math.MaxInt32
|
||||
|
||||
var recurse func(x, y, pathLength int)
|
||||
recurse = func(x, y, pathLength int) {
|
||||
|
||||
+3
-2
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -94,7 +95,7 @@ func MakeDijkstraGrid(grid [][]string, startCoords [2]int) *DijkstraGrid {
|
||||
for col, cellString := range rowSli {
|
||||
finalGrid[row][col] = &dijkstraNode{
|
||||
value: cellString,
|
||||
distance: 1<<31 - 1, // maximum safe integer, effectively 2^31 - 1
|
||||
distance: math.MaxInt32,
|
||||
keysFound: map[string]bool{startKey: true}, // initialize with the starting key
|
||||
keysNeeded: make(map[string]bool), // empty map for now
|
||||
seen: false, // initialize as false
|
||||
@@ -212,7 +213,7 @@ func (graph *Graph) dfsMinmumDistance() int {
|
||||
// inputs are: 1. the entry point (the key to generate a path FROM)
|
||||
// 2. the keys that have been found so far
|
||||
traverse = func(entry string, keysFound map[string]bool) int {
|
||||
shortestFromThisNode := 1<<31 - 1
|
||||
shortestFromThisNode := math.MaxInt32
|
||||
|
||||
cacheKey := makeCacheKey(entry, keysFound, len(graph.keysToKeys)-1)
|
||||
|
||||
|
||||
+3
-2
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
@@ -100,7 +101,7 @@ func MakeDijkstraGrid(grid [][]string, startCoords [2]int) *DijkstraGrid {
|
||||
for col, cellString := range rowSli {
|
||||
finalGrid[row][col] = &dijkstraNode{
|
||||
value: cellString,
|
||||
distance: 1<<31 - 1, // maximum safe integer, effectively 2^31 - 1
|
||||
distance: math.MaxInt32,
|
||||
keysFound: map[string]bool{startKey: true}, // initialize with the starting key
|
||||
keysNeeded: make(map[string]bool), // empty map for now
|
||||
seen: false, // initialize as false
|
||||
@@ -223,7 +224,7 @@ func (graph *Graph) dfsMinmumDistance() int {
|
||||
// inputs are: 1. the entry point (the key to generate a path FROM)
|
||||
// 2. the keys that have been found so far
|
||||
traverse = func(entry string, keysFound map[string]bool) int {
|
||||
shortestFromThisNode := 1<<31 - 1
|
||||
shortestFromThisNode := math.MaxInt32
|
||||
|
||||
cacheKey := makeCacheKey(entry, keysFound, graph.allKeysNeeded)
|
||||
|
||||
|
||||
+4
-6
@@ -3,12 +3,10 @@ package main
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// large value to act as "infinite distance away" when setting up a dijkstra grid
|
||||
const bigSafeInt = 1 << 30 // 2^30
|
||||
|
||||
func main() {
|
||||
input := util.ReadFile("../input.txt")
|
||||
lines := strings.Split(string(input), "\n")
|
||||
@@ -63,12 +61,12 @@ func MakeDijkstraGrid(inputGrid [][]string) *Dijkstra {
|
||||
// make a node for each cell
|
||||
switch value := inputGrid[row][col]; value {
|
||||
case "#": // wall
|
||||
grid[row-2][col-2] = &Node{"#", bigSafeInt, "", [2]int{0, 0}}
|
||||
grid[row-2][col-2] = &Node{"#", math.MaxInt32, "", [2]int{0, 0}}
|
||||
// if this is a hallway node, use a helper function to determine if there this is a portal
|
||||
case ".": // hallway
|
||||
hallwayNode := &Node{
|
||||
value: ".",
|
||||
distance: bigSafeInt,
|
||||
distance: math.MaxInt32,
|
||||
portalName: "",
|
||||
jumpCoordinates: [2]int{0, 0},
|
||||
}
|
||||
@@ -164,7 +162,7 @@ func (dijkstra *Dijkstra) handleFrontOfQueue() (done bool) {
|
||||
isInbounds := nextRow >= 0 && nextRow < len(dijkstra.grid) && nextCol >= 0 && nextCol < len(dijkstra.grid[0])
|
||||
if isInbounds {
|
||||
// if the nextNode is a hallway & has not been traveled to yet
|
||||
if nextNode := dijkstra.grid[nextRow][nextCol]; nextNode != nil && nextNode.value == "." && nextNode.distance == bigSafeInt {
|
||||
if nextNode := dijkstra.grid[nextRow][nextCol]; nextNode != nil && nextNode.value == "." && nextNode.distance == math.MaxInt32 {
|
||||
// update the distance of the nextNode
|
||||
nextNode.distance = currentNode.distance + 1
|
||||
// add its coordinates to the queue
|
||||
|
||||
+3
-12
@@ -6,9 +6,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// large value to act as "infinite distance away" when setting up a dijkstra grid
|
||||
const bigSafeInt = 1 << 30 // 2^30
|
||||
|
||||
func main() {
|
||||
input := util.ReadFile("../input.txt")
|
||||
lines := strings.Split(string(input), "\n")
|
||||
@@ -144,13 +141,9 @@ func (dijkstra *DijkstraRecursive) AddLayer() (layerCount int) {
|
||||
for row := 0; row < len(sanitizedGrid); row++ {
|
||||
grid[row] = make([]*Node, len(sanitizedGrid[0]))
|
||||
for col := 0; col < len(sanitizedGrid); col++ {
|
||||
switch value := sanitizedGrid[row][col]; value {
|
||||
case "#":
|
||||
grid[row][col] = &Node{"#", bigSafeInt, "", [3]int{0, 0, 0}}
|
||||
switch value := sanitizedGrid[row][col]; value
|
||||
case ".":
|
||||
grid[row][col] = &Node{
|
||||
value: ".",
|
||||
distance: bigSafeInt,
|
||||
grid[row][col] = &Node
|
||||
}
|
||||
// get portal name and jump coord from maps if applicable
|
||||
portalName, found := dijkstra.mapCoordsToPortals[[2]int{row, col}]
|
||||
@@ -198,9 +191,7 @@ func (dijkstra *DijkstraRecursive) handleFrontOfQueue() (done bool) {
|
||||
for i := 0; i < 4; i++ {
|
||||
nextRow, nextCol := row+dRow[i], col+dCol[i]
|
||||
isInbounds := nextRow >= 0 && nextRow < len(currentLayersGrid) && nextCol >= 0 && nextCol < len(currentLayersGrid[0])
|
||||
if isInbounds {
|
||||
// if the nextNode is a hallway & has not been traveled to yet
|
||||
if nextNode := currentLayersGrid[nextRow][nextCol]; nextNode != nil && nextNode.value == "." && nextNode.distance == bigSafeInt {
|
||||
if isInbounds
|
||||
// update the distance of the nextNode
|
||||
nextNode.distance = currentNode.distance + 1
|
||||
// add its coordinates to the queue, will always be on the same layer b/c this is NOT handling jumps
|
||||
|
||||
+2
-1
@@ -86,7 +86,8 @@ func getBiodiversity(grid [][]string) int {
|
||||
for i, row := range grid {
|
||||
for j, val := range row {
|
||||
// dumb cheeky way to get power of two... 1 is 2^0, then shift the power
|
||||
powerOfTwo := 1 << (5*i + j) // 1 << 0 == 2^0 == 1; 1 << 1 == 2^1
|
||||
// 1 << 0 == 2^0 == 1; 1 << 1 == 2^1
|
||||
powerOfTwo := 1 << (5*i + j)
|
||||
if val == "#" {
|
||||
biodiversity += powerOfTwo
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
package util
|
||||
|
||||
const MaxSafeInt int = 1<<31 - 1
|
||||
@@ -1,12 +0,0 @@
|
||||
/*
|
||||
just a test to see how building other parts of a package works
|
||||
*/
|
||||
|
||||
package util
|
||||
|
||||
import "fmt"
|
||||
|
||||
// OtherFunc is a test :p
|
||||
func OtherFunc() {
|
||||
fmt.Println("hello from other func")
|
||||
}
|
||||
Reference in New Issue
Block a user