This commit is contained in:
Karl Spickermann 2022-12-12 23:26:26 +01:00
parent f3cb16ac9a
commit 6401908349
2 changed files with 131 additions and 70 deletions

@ -5,43 +5,44 @@ import (
"fmt"
)
type ActivePath struct {
activePosition [2]int
visitedPositions [][2]int
steps int
}
type FieldPoint struct {
cost int
height int
before [2]int
}
func main() {
//args := os.Args[1:]
lines := helper.ReadTextFile("day12/input")
lines := helper.ReadTextFile("day12/inputFichte")
start, end := getStartingPointAndEndpoint(lines)
fmt.Println(start)
fmt.Println(end)
field := getField(lines)
activePaths := []ActivePath{{start, [][2]int{}, 0}}
fastestRoute := 999999999
for len(activePaths) > 0 {
newPaths := []ActivePath{}
for _, path := range activePaths {
if path.activePosition == [2]int{2, 6} {
//fmt.Println("here")
}
newPathStep := path.step(field)
if path.activePosition == end && fastestRoute > path.steps {
fastestRoute = path.steps
path.print(field)
} else if path.steps < fastestRoute {
newPaths = append(newPaths, newPathStep...)
}
part1(field, end)
print(field)
//fastestPath := part2(lines, field, end)
}
func part2(lines []string, field [][]FieldPoint, end [2]int) {
lowestPoints := getLowestPoints(lines)
fastestPath := 999999
for _, point := range lowestPoints {
activePoints := make(map[[2]int]struct{})
activePoints[[2]int{point[0], point[1]}] = struct{}{}
field = getField(lines)
steps := getBestRouteLength(activePoints, field, end)
if steps < fastestPath && steps != 0 {
fastestPath = steps
}
activePaths = newPaths
}
fmt.Println(fastestRoute)
fmt.Println(fastestPath)
}
func part1(field [][]FieldPoint, end [2]int) {
activePoints := make(map[[2]int]struct{})
activePoints[[2]int{0, 0}] = struct{}{}
fmt.Println(getBestRouteLength(activePoints, field, end) + 2)
}
func getField(lines []string) [][]FieldPoint {
@ -57,35 +58,50 @@ func getField(lines []string) [][]FieldPoint {
if line[j] == 'E' {
height = int('z')
}
fieldLine[j] = FieldPoint{0, height}
fieldLine[j] = FieldPoint{0, height, [2]int{-1, -1}}
}
field[i] = fieldLine
}
return field
}
func (path *ActivePath) step(field []string) (newPaths []ActivePath) {
if getHeight(path.activePosition, field) == 'E' {
return
func getBestRouteLength(activePoints map[[2]int]struct{}, field [][]FieldPoint, endPoint [2]int) int {
for len(activePoints) > 0 {
step(&field, &activePoints)
}
nextSteps := path.getNextStep(field)
for _, step := range nextSteps {
newPath := ActivePath{step, append(path.visitedPositions, path.activePosition), path.steps + 1}
newPaths = append(newPaths, newPath)
}
return
return field[endPoint[0]][endPoint[1]].cost
}
func (path *ActivePath) checkVisitable(direction [2]int, field []string) (visitable bool) {
toVisit := path.activePosition
func step(field *[][]FieldPoint, activePoints *map[[2]int]struct{}) {
point := get_some_key(*activePoints)
delete(*activePoints, point)
current0 := point[0]
current1 := point[1]
currentFieldSumCost := (*field)[current0][current1].cost
directions := [4][2]int{{0, -1}, {+1, 0}, {0, 1}, {-1, 0}}
for _, direction := range directions {
toVisit0 := current0 + direction[0]
toVisit1 := current1 + direction[1]
if checkVisitable(point, direction, *field) {
cost := 1 + currentFieldSumCost
if (*field)[toVisit0][toVisit1].cost > cost || (*field)[toVisit0][toVisit1].cost == 0 {
(*field)[toVisit0][toVisit1].cost = cost
(*field)[toVisit0][toVisit1].before = [2]int{current0, current1}
(*activePoints)[[2]int{toVisit0, toVisit1}] = struct{}{}
}
}
}
}
func checkVisitable(activePosition [2]int, direction [2]int, field [][]FieldPoint) (visitable bool) {
toVisit := activePosition
toVisit[0] += direction[0]
toVisit[1] += direction[1]
if toVisit[0] > -1 && toVisit[1] > -1 &&
toVisit[0] < len(field) && toVisit[1] < len((field)[0]) {
if helper.Contains2Int(path.visitedPositions, toVisit) {
return false
}
heightToVisit := getHeight(toVisit, field)
heightCurrentPos := getHeight(path.activePosition, field) + 1
toVisit[0] < len(field) && toVisit[1] < len((field)[0]) &&
field[activePosition[0]][activePosition[1]].before != toVisit {
heightToVisit := field[toVisit[0]][toVisit[1]].height
heightCurrentPos := field[activePosition[0]][activePosition[1]].height + 1
if heightToVisit > heightCurrentPos {
return false
}
@ -94,27 +110,6 @@ func (path *ActivePath) checkVisitable(direction [2]int, field []string) (visita
return false
}
func (path ActivePath) getNextStep(field []string) (nextSteps [][2]int) {
directions := [][2]int{{0, -1}, {0, +1}, {-1, 0}, {+1, 0}}
for _, direction := range directions {
if path.checkVisitable(direction, field) {
newStep := [2]int{path.activePosition[0] + direction[0], path.activePosition[1] + direction[1]}
nextSteps = append(nextSteps, newStep)
}
}
return
}
func getHeight(location [2]int, field []string) int {
if field[location[0]][location[1]] == 'S' {
return 'a'
}
if field[location[0]][location[1]] == 'E' {
return 'z'
}
return int(field[location[0]][location[1]])
}
func getStartingPointAndEndpoint(field []string) (start, end [2]int) {
for i := 0; i < len(field); i++ {
for j := 0; j < len(field[i]); j++ {
@ -129,16 +124,41 @@ func getStartingPointAndEndpoint(field []string) (start, end [2]int) {
return
}
func (path ActivePath) print(field []string) {
func getLowestPoints(field []string) [][2]int {
lowestPoints := [][2]int{}
for i := 0; i < len(field); i++ {
tempArr := ""
for j := 0; j < len(field[i]); j++ {
if helper.Contains2Int(path.visitedPositions, [2]int{i, j}) {
tempArr += "#"
} else {
tempArr += "."
if field[i][j] == 'a' {
lowestPoints = append(lowestPoints, [2]int{i, j})
}
}
fmt.Println(tempArr)
}
return lowestPoints
}
func get_some_key(m map[[2]int]struct{}) [2]int {
for k := range m {
return k
}
return [2]int{-1, -1}
}
func print(field [][]FieldPoint) {
for i := 0; i < len(field); i++ {
fieldLine := field[i]
tempArray := []int{}
for j := 0; j < len(fieldLine); j++ {
tempArray = append(tempArray, fieldLine[j].height-96)
}
fmt.Println(tempArray)
}
fmt.Println()
for i := 0; i < len(field); i++ {
fieldLine := field[i]
tempArray := []int{}
for j := 0; j < len(fieldLine); j++ {
tempArray = append(tempArray, fieldLine[j].cost)
}
fmt.Println(tempArray)
}
}

41
day12/inputFichte Normal file

@ -0,0 +1,41 @@
abcccccccccaaaaaaaaaaccccccccccccaaaaaaaaccaaccccccccccccccccccccccccccccccccccccccccccccaaaaaa
abccccccccccaaaaaaaaaccccccccccccaaaaaaaaaaaacccccccccccaacccacccccccccccccccccccccccccccaaaaaa
abcccccccccccaaaaaaacccccccccccccaaaaaaaaaaaaaacccccccccaaacaacccccccccaaaccccccccccccccccaaaaa
abccccccccccaaaaaaccccccccccccccaaaaaaaaaaaaaaaccccccccccaaaaaccccccccccaaacccccccccccccccccaaa
abccccccccccaaaaaaaccccccccccccaaaaaaaaaaaaaacccccccccccaaaaaacccccccccaaaacccccccccccccccccaac
abaaccaaccccaaccaaaccccccccaaaaaaaaaaaaaaacaaccccccccccaaaaaaaacccccccccaaalcccccccccccccccaaac
abaaaaaacccccccccaaccccccccaaaaaacccaaaacccaaccccccccccaaaaaaaaccccccccalllllllcccccccccccccccc
abaaaaaacccccccaaacccccccccaaaaccccccaaaccccaaaaacccccccccaacccccccaaaakllllllllcccccccaacccccc
abaaaaaacccccccaaaacccccccccaacccccccaaaccccaaaaacccccccccaacccccccaakkklllpllllccccacaaacccccc
abaaaaaaaccccccaaaaccccaaccccccccccccccccccaaaaaaccccccccccccccccccckkkkpppppplllcccaaaaaaacccc
abaaaaaaacaaaccaaaaccaaaaaaccccccccccccccccaaaaaacccccccaaaccccckkkkkkkpppppppplllcddaaaaaacccc
abcaaaacccaacccccccccaaaaaacccccaaaccccccccaaaaaacccccccaaaaccjkkkkkkkpppppuppplmmdddddaaaccccc
abccaaaaaaaaaccccccccaaaaaaccccaaaaaacccccccaaacccccccccaaaajjjkkkkkrpppuuuuupppmmmdddddacccccc
abccccaaaaaaaacccccccaaaaacccccaaaaaacccccccccccccccccccaaacjjjjrrrrrrppuuuuupqqmmmmmddddaccccc
abccccaaaaaaaaacccccccaaaacccccaaaaaaccccccccccccccccccccccjjjrrrrrrrrpuuuxuvvqqqmmmmmddddccccc
abccccaaaaaaaaacccccccccccccccccaaaaaccccaacccaccccccccaaccjjjrrrruuuuuuuxxyvvqqqqqmmmmmdddcccc
abccccaaaaaaaacccccccccaaaccccccaacaaccccaaacaacccaaacaaaccjjjrrrtuuuuuuuxxyvvvqqqqqmmmmdddcccc
abccaaaaaaaacccccccccccaaaaaccccccccccccccaaaaacccaaaaaaaccjjjrrttttxxxxxxyyvvvvvqqqqmmmmdeeccc
abccaaaccaaaccccccccaacaaaaacccccccccccccaaaaaacccaaaaaacccjjjrrtttxxxxxxxyyvvvvvvvqqqmmmeeeccc
abaaaaaaaaaacccaaaccaaaaaaaaaaaccaaaccccaaaaaaaacccaaaaaaaajjjqqrttxxxxxxxyyyyyyvvvqqqnnneeeccc
SbaaaaaaaaccccaaaaccaaaaaaaaaaaaaaaaacccaaaaaaaaccaaaaaaaaacjjjqqtttxxxxEzzyyyyvvvvqqqnnneeeccc
abcaaaaaacccccaaaaccccaaaaaaaccaaaaaaccccccaaccccaaaaaaaaaaciiiqqqtttxxxyyyyyyvvvvrrrnnneeecccc
abcaaaaaacccccaaaacccaaaaaaaaccaaaaaaccccccaaccccaaacaaacccciiiqqqqttxxyyyyyywvvvrrrnnneeeecccc
abcaaaaaaccccccccccccaaaaaaaaacaaaaacccccccccccccccccaaaccccciiiqqtttxxyyyyyywwrrrrnnnneeeccccc
abcaaacaacccccaacccccaaaaaaaaacaaaaacccccccccccccccccaaaccccciiiqqttxxxywwyyywwrrrnnnneeecccccc
abccccccccaaacaaccccccccccacccccccccccccccccccccccccccccccccciiqqqttxxwwwwwwywwrrrnnneeeccccccc
abccaacccccaaaaaccccccccccccccccccccccccccccccccccccccccaacaaiiqqqttwwwwsswwwwwrrrnnfffeccccccc
abaaaaccccccaaaaaacccccccccccccccccccccccccccccaaaccccccaaaaaiiqqqttssssssswwwwrrronfffaccccccc
abaaaaaacccaaaaaaacccccccccccccccccccccccccccaaaaaacccccaaaaaiiqqqssssssssssswrrrooofffaaaacccc
abaaaaaaccaaaaaacccccccccccccccccccccccccccccaaaaaacccccaaaaaiiqqqppssspppssssrrrooofffaaaacccc
abaaaaaaccaacaaacccccccccccccccccccccccccccccaaaaaacccccaaaaaiihpppppppppppossrrooofffaaaaacccc
abaaaaccccccccaacccccccccccccccccccccccccccccaaaaaccccccccaaahhhhppppppppppoooooooofffaaaaccccc
abaaaaccccccccccaacccccccccccccccccaaacccccccaaaaacccccccccccchhhhhhhhhhggpoooooooffffaaaaccccc
abccaacccccccacaaaccccccccccccccccaaaaacccccccccccccccccccccccchhhhhhhhhggggoooooffffaacaaacccc
abccccccccccaaaaacaaccccccccccccccaaaaaccccccccccccccccccccccccchhhhhhhhggggggggggffcaacccccccc
abccccccccccaaaaaaaaccccccccccccccaaaacccaacccccccccccaccccccccccccccaaaaaggggggggfcccccccccccc
abccccccccccccaaaaaccccaacccccccccaaaacaaaaccccccccaaaaccccccccccccccaaaacaaagggggcccccccccaccc
abcccccccccccaaaaacccccaacccccccccaaaaaaaaaccccccccaaaaaaccccccccccccaaaccaaaacccccccccccccaaac
abcccccccccccaacaaccaaaaaaaacccaaaaaaaaaaaccccccccccaaaaccccccccccccccaccccaaacccccccccccccaaaa
abccccccccccccccaaccaaaaaaaaccaaaaaaaaaaaccccccccccaaaaacccccccccccccccccccccacccccccccccccaaaa
abccccccccccccccccccccaaaaacccaaaaaaaaaaaacccccccccaacaacccccccccccccccccccccccccccccccccaaaaaa