|
@@ -12,7 +12,7 @@ type Point struct {
|
|
|
value byte
|
|
|
}
|
|
|
|
|
|
-func key(p Point) string {
|
|
|
+func (p *Point) key() string {
|
|
|
return fmt.Sprintf("%d_%d", p.y, p.x)
|
|
|
}
|
|
|
|
|
@@ -50,7 +50,7 @@ func getMoves(reindeer Point, matrix [][]byte, xMax, yMax int) []Point {
|
|
|
return moves
|
|
|
}
|
|
|
|
|
|
-func hike(reindeer Point, matrix [][]byte, xMax, yMax int, hash func(Point) string) int {
|
|
|
+func hike(reindeer Point, matrix [][]byte, xMax, yMax int, oneWay bool) int {
|
|
|
var nines int
|
|
|
visited := make(map[string]bool)
|
|
|
|
|
@@ -64,9 +64,13 @@ func hike(reindeer Point, matrix [][]byte, xMax, yMax int, hash func(Point) stri
|
|
|
|
|
|
newMoves := getMoves(current, matrix, xMax, yMax)
|
|
|
for _, newMove := range newMoves {
|
|
|
- if !visited[hash(newMove)] {
|
|
|
+ if oneWay {
|
|
|
+ if !visited[newMove.key()] {
|
|
|
+ moves = append(moves, newMove)
|
|
|
+ visited[newMove.key()] = true
|
|
|
+ }
|
|
|
+ } else {
|
|
|
moves = append(moves, newMove)
|
|
|
- visited[hash(newMove)] = true
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -74,7 +78,7 @@ func hike(reindeer Point, matrix [][]byte, xMax, yMax int, hash func(Point) stri
|
|
|
return nines
|
|
|
}
|
|
|
|
|
|
-func part1(matrix [][]byte) int {
|
|
|
+func solve(matrix [][]byte, oneWay bool) int {
|
|
|
var result int
|
|
|
xMax := len(matrix[0])
|
|
|
yMax := len(matrix)
|
|
@@ -83,7 +87,7 @@ func part1(matrix [][]byte) int {
|
|
|
for x := range matrix[y] {
|
|
|
if matrix[y][x] == '0' {
|
|
|
reindeer := Point{x: x, y: y, value: '0'}
|
|
|
- result += hike(reindeer, matrix, xMax, yMax, key)
|
|
|
+ result += hike(reindeer, matrix, xMax, yMax, oneWay)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -91,42 +95,6 @@ func part1(matrix [][]byte) int {
|
|
|
return result
|
|
|
}
|
|
|
|
|
|
-func hike2(reindeer Point, matrix [][]byte, xMax, yMax int) int {
|
|
|
- var nines int
|
|
|
-
|
|
|
- moves := []Point{reindeer}
|
|
|
- for len(moves) > 0 {
|
|
|
- current := moves[0]
|
|
|
- moves = moves[1:]
|
|
|
- if matrix[current.y][current.x] == '9' {
|
|
|
- nines++
|
|
|
- }
|
|
|
-
|
|
|
- newMoves := getMoves(current, matrix, xMax, yMax)
|
|
|
- for _, newMove := range newMoves {
|
|
|
- moves = append(moves, newMove)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return nines
|
|
|
-}
|
|
|
-
|
|
|
-func part2(matrix [][]byte) int {
|
|
|
- var result int
|
|
|
- xMax := len(matrix[0])
|
|
|
- yMax := len(matrix)
|
|
|
-
|
|
|
- for y := range matrix {
|
|
|
- for x := range matrix[y] {
|
|
|
- if matrix[y][x] == '0' {
|
|
|
- reindeer := Point{x: x, y: y, value: '0'}
|
|
|
- result += hike2(reindeer, matrix, xMax, yMax)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return result
|
|
|
-}
|
|
|
func main() {
|
|
|
if len(os.Args) < 2 {
|
|
|
log.Fatal("You need to specify a file!")
|
|
@@ -139,6 +107,6 @@ func main() {
|
|
|
}
|
|
|
|
|
|
matrix := readInput(file)
|
|
|
- fmt.Println("Part1:", part1(matrix))
|
|
|
- fmt.Println("Part2:", part2(matrix))
|
|
|
+ fmt.Println("Part1:", solve(matrix, true))
|
|
|
+ fmt.Println("Part2:", solve(matrix, false))
|
|
|
}
|