-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Lewis
committed
Dec 21, 2024
1 parent
35e609b
commit 345151a
Showing
3 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
208A | ||
586A | ||
341A | ||
463A | ||
593A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cdlewis/advent-of-code/util/aoc" | ||
"github.com/cdlewis/advent-of-code/util/grid" | ||
) | ||
|
||
var testInput = `029A | ||
980A | ||
179A | ||
456A | ||
379A` | ||
|
||
var keypad = grid.Grid[byte]([][]byte{ | ||
{'7', '8', '9'}, | ||
{'4', '5', '6'}, | ||
{'1', '2', '3'}, | ||
{'#', '0', 'A'}, | ||
}) | ||
|
||
var directionPad = [][]byte{ | ||
{'#', '^', 'A'}, | ||
{'<', 'v', '>'}, | ||
} | ||
|
||
var changeToDirection = map[grid.Point]byte{ | ||
grid.UP: '^', | ||
grid.DOWN: 'v', | ||
grid.LEFT: '<', | ||
grid.RIGHT: '>', | ||
} | ||
|
||
func main() { | ||
input := strings.Split(aoc.GetInput(21, false, testInput), "\n") | ||
|
||
total := 0 | ||
for _, row := range input { | ||
key, _ := strconv.Atoi(row[:len(row)-1]) | ||
complexity := cost(row, -1) | ||
total += (key * complexity) | ||
} | ||
|
||
fmt.Println(total == 195664513288128) | ||
} | ||
|
||
var cache = map[string]int{} | ||
|
||
func cost(code string, robot int) int { | ||
if result, ok := cache[code+strconv.Itoa(robot)]; ok { | ||
return result | ||
} | ||
|
||
maze := keypad | ||
if robot > 0 { | ||
maze = directionPad | ||
} | ||
|
||
totalCost := 0 | ||
prev := 'A' | ||
for _, curr := range code { | ||
posA, _ := maze.Find(byte(prev)) | ||
posB, _ := maze.Find(byte(curr)) | ||
|
||
paths := allPaths(posA, posB, maze) | ||
cheapestPath := math.MaxInt | ||
for _, p := range paths { | ||
if robot == -1 { | ||
length := cost(p, 25) | ||
if length < cheapestPath { | ||
cheapestPath = length | ||
} | ||
} else if robot > 1 { | ||
length := cost(p, robot-1) | ||
if length < cheapestPath { | ||
cheapestPath = length | ||
} | ||
} else { | ||
if len(p) < cheapestPath { | ||
cheapestPath = len(p) | ||
} | ||
} | ||
} | ||
|
||
totalCost += cheapestPath | ||
prev = curr | ||
} | ||
|
||
cache[code+strconv.Itoa(robot)] = totalCost | ||
return totalCost | ||
} | ||
|
||
type item struct { | ||
path []grid.Point | ||
seen []grid.Point | ||
} | ||
|
||
func allPaths( | ||
a grid.Point, | ||
b grid.Point, | ||
maze grid.Grid[byte], | ||
) []string { | ||
var results [][]grid.Point | ||
q := []item{{seen: []grid.Point{a}}} | ||
|
||
for len(q) > 0 { | ||
curr := q[0] | ||
currHead := curr.seen[len(curr.seen)-1] | ||
q = q[1:] | ||
|
||
if currHead == b { | ||
results = append(results, curr.path) | ||
} | ||
|
||
for _, d := range grid.Directions { | ||
nextPos := currHead.Add(d) | ||
if maze.GetOrElse(nextPos, '#') == '#' { | ||
continue | ||
} | ||
|
||
if slices.Index(curr.seen, nextPos) != -1 { | ||
continue | ||
} | ||
|
||
q = append(q, item{ | ||
path: append(slices.Clone(curr.path), d), | ||
seen: append(slices.Clone(curr.seen), nextPos), | ||
}) | ||
} | ||
} | ||
|
||
var adaptedResults []string | ||
for _, r := range results { | ||
var adaptedResult []byte | ||
for _, i := range r { | ||
adaptedResult = append(adaptedResult, changeToDirection[i]) | ||
} | ||
adaptedResult = append(adaptedResult, 'A') | ||
|
||
adaptedResults = append(adaptedResults, string(adaptedResult)) | ||
} | ||
|
||
return adaptedResults | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters