day24; slow, but working; probably some pruning possible, but I didn't bother
This commit is contained in:
parent
d2defd64e6
commit
91dcccf17f
59
day24/day.go
Normal file
59
day24/day.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package day24
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getInputGrid(inp []string) (tools.GridToggle, map[int]tools.Coordinate) {
|
||||||
|
grid := tools.NewGridToggle()
|
||||||
|
targets := make(map[int]tools.Coordinate)
|
||||||
|
|
||||||
|
for y, line := range inp {
|
||||||
|
for x, c := range line {
|
||||||
|
if c == '#' {
|
||||||
|
grid.Set(x, y, tools.On)
|
||||||
|
} else if c != '.' {
|
||||||
|
targets[int(c-48)] = tools.Coordinate{X: x, Y: y}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return grid, targets
|
||||||
|
}
|
||||||
|
|
||||||
|
func findShortestPathBetweenNodes(grid tools.GridToggle, targets map[int]tools.Coordinate, startNode int, alreadyVisited tools.Set, pathLen int, part2 bool) int {
|
||||||
|
alreadyVisited.Add(startNode)
|
||||||
|
minLen := math.MaxInt32
|
||||||
|
for node, target := range targets {
|
||||||
|
if alreadyVisited.Contains(node) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
thisLen := len(grid.GetPathAStar(targets[startNode], target, false)) - 1
|
||||||
|
if alreadyVisited.Len() == len(targets)-1 {
|
||||||
|
if part2 {
|
||||||
|
return thisLen + pathLen + len(grid.GetPathAStar(target, targets[0], false)) - 1
|
||||||
|
} else {
|
||||||
|
return thisLen + pathLen
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
thisLen = findShortestPathBetweenNodes(grid, targets, node, alreadyVisited.Copy(), thisLen, part2)
|
||||||
|
if thisLen < minLen {
|
||||||
|
minLen = thisLen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return minLen + pathLen
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
||||||
|
grid, targets := getInputGrid(puzzle.GetInputArray())
|
||||||
|
|
||||||
|
return findShortestPathBetweenNodes(grid, targets, 0, tools.NewSet(), 0, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
||||||
|
grid, targets := getInputGrid(puzzle.GetInputArray())
|
||||||
|
|
||||||
|
return findShortestPathBetweenNodes(grid, targets, 0, tools.NewSet(), 0, true)
|
||||||
|
}
|
||||||
39
inputs/24
Normal file
39
inputs/24
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#######################################################################################################################################################################################
|
||||||
|
#...........#.....#...........#.#.......#.....#.#...............#.....#.....#.......#.......#.......#.....................#.........#.....#...#3......#...#.#.............#.......#...#
|
||||||
|
#####.#.#.###.###.#####.#.#####.#.###.###.###.#.#.#.#.#.#.###.#.###.###.###.#.#######.#.#.#.###.###.#.#.#.#####.#.#.#####.###.#.#######.#############.#.#.#.#.#.#.###.#.#.#.#.###.#.#.#
|
||||||
|
#...#.#.#.#.....#.#...#...#.....#.....#.#...#.........#.....................#.....#...#...#.......#.....#.#.........#.#.#.#.#.......................#...#...#.#.#.#.....#.#.........#.#
|
||||||
|
#.#.#.#.#.#####.#.#.#.#.#.#.#.#.###.#.#.###.#.#.#.#.#.#########.#.###.###.#.#.###.###.#.#.#.#.###.#.#.#.#.#.#.#####.#.#.###.###.#######.#.#.#.###.#.###.#.#.#####.#####.#.#.###.#.#.#.#
|
||||||
|
#.#.#...#.....#.#.....#...#.#...#.#...#.......#...........#...#...#...#.#.....#.#...#...........#.#.......#...#...#.#.........#...#.......#.#...#.....#...#.......#.#.#.............#.#
|
||||||
|
#.#######.#.#.###.#.#.#####.#.#.#.#.###.#####.#.#.###.#.#.#.#.#.#.#.#.#.#.###.#.#.#.###.#######.#######.#.#.###.#.#.#.#.#.#.###.#.#.#.#.#.###.#.#####.#.#.#.#.#.#.#.#.#.###.#.#####.#.#
|
||||||
|
#.#...#.....#.......#.......#.#.......#...#.#.....#.#.#.#.#.#.......#...#.....#.#.#.#.#.........#...#...#...#.......#...#...#.....#.....#...#.........#...#.#...#...#...#.........#.#.#
|
||||||
|
#.#.#.#.###.###.#.#.#.###.###.#.#.###.#.#.#.#####.#.###.#.#####.#####.###.#.#.#.#.###.#.#.#.###.###.#.#.#.#.#.#####.#.###.#.#.###.#.#.#.#.###.###.#######.#.###.###.#.#######.#.#.#.#.#
|
||||||
|
#.#...#...#...............#...#...#...#.#.........#.#...#...#.#...#.#.....#...#.#...#.#.#...#.#.#.#.#.....#...#...#.......#.#...#.........#.#.....#.#...#.....#.#...#.#2#.....#.......#
|
||||||
|
#.#.#####.#########.#.###.#.#####.#.#.#.#######.#.###.#.#.#.#.###.#.#.###.#.###.#.#.#.#.#.###.###.#.#.#######.#.###.#.#.#.#.#.#.#.#####.###.#.###.#.#####.#.#.###.#.#.#.###.#.###.###.#
|
||||||
|
#...#...........#.....#.......#...#0..........#...#.....#.#...#.#...#.#.#...#.#.......#.........#...#.#...#.#.#.......#...#.#.........#.#.....................#.#.#.#.....#.#.....#...#
|
||||||
|
#.###.#.#.#.#.#.###.#.#.#.#.#.###.#.#.###.#.#.#.###.###.#.#####.#.#.#.#.###.#.#.###.#.#.###.###.#.#####.#.#.#.###.#.#######.#######.#.#.#.###.#.#.#.#.#.#######.#.#.#####.#.###.###.#.#
|
||||||
|
#...#...#...#.#...........#...#.#.....#...#...#.....#.#.............#...#.........#...#.....#...#.......#...#.#.............#.........#...#.#.....#.#.........#.............#...#.....#
|
||||||
|
#.#.#.###.#.###.#.#.#.###.#.###.#######.#.#.###.#.#.#.#.###.#.###.#.#.#.#.#####.#.#.#.#####.#.#####.#.#.#.#.###.###.#.###.###.###.#.#.#.###.#.#.###.#.#.###.#########.#####.#.#.#.#####
|
||||||
|
#.#...#.#.......#...#.....#.....#...#...#.....#...#...#.........#.....#.#.........#.....#.....#.........#...#.#.....#.#...#...#.#.#...#.#...#.........#.#...#.........#.#.......#.....#
|
||||||
|
#.#####.#.###.#.#.#.###.#.#.#.#.#.#.###.#.#.#.###.###.#.###.#.###.#.#.#.###.#.#####.###.#.#.#.#.#.#####.#.#.#.#.#.#.###.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#.#.#.#.#.#.#.###.#.#.#
|
||||||
|
#.....#...#...........#...#...........#.#.....#...#.....#...#.#.#.#...#.....#...#...#...............#...#.........#...#.....#...#.#...#.#...#.....#.....#.....#...#.#...#...#.#.......#
|
||||||
|
###.###.#.#.#####.###.#####.#.###.#.#.#####.#.###.#.###.#.###.#.#.#.#.###.#.###.#.#.#.#.###.#.###.#.#.#.#.#.#.###.#.#.#.###.###.#.#.###.#########.#.#####.###########.#.###.###.#####.#
|
||||||
|
#...#1#...#...#.......#...#...#.#...#...#...........#.#.......#.......#.#.........#.#...#...#...............#...#.....#.#...#.#.....#.........#...#.....#.#...#...#.....#.#.#.#...#...#
|
||||||
|
#.###.###.#.#.#.#####.#.#.#.#.#.#.#####.#####.#.#####.#.#.#.#.###.#.#.#.###.#.###.#.###.#.#.#####.#.#.#####.#.#.###.#.###.#.#.#.#.#.#.#####.#.#.#.#.###.#.#.###.#.#.#.#.#.#.#.###.#.#.#
|
||||||
|
#.....#.#.#.#.........#.#.#.....#.....#...#.....#...#.............#...#...#.....#...#.......#.......#.#.....#...#...#.#...#.....#.#...#.....#.#.#.#.#.#.#.......#...........#.#5#.....#
|
||||||
|
#.###.#.#.###.###.###.#.#####.#.#.###.###.#.###.#############.#######.#.###.#.###.#.#####.#.#.#.#####.#.#.#####.#.#.#.#.#.#######.#.#.###########.#.#.#.#.#.#.#.#.#.###.#.#.#.#.#.###.#
|
||||||
|
#.....#...#...................#.......#...#...#.#.....#.............#.....#...#.....#.......#.#.#.........#.........#.......#.......#...#.........#.#...#...#.....#.#.....#...#...#.#.#
|
||||||
|
#.###.###.###########.###.#####.#####.###.#.#.#.#.#.#.###.#.###.#.#######.#.#.#.#.#.#.###.#.#.#####.#.#.#.#######.#.###.#.#.###.#.#.#.###.#####.#####.#.#.#.#.###.#.###.###########.#.#
|
||||||
|
#.#.#.#...#...#.....#.....#.#.......#.....#.....#...........#.#...#.........#.#.#.#.....#.#.#...........#...#...#...#.......#...#.........#.....#.....#.....#.......#...#...#.#...#...#
|
||||||
|
#.#.#.#.#.#########.#.#.###.#.#.###.###.#.#.#.###.#####.#.#.#.#.#.#.#######.#.#.#######.#.###.#.###.###.#.###.#.#.#.#######.#######.#.#.#.#####.#.###.#.#####.#.###.#.#.###.#.#######.#
|
||||||
|
#.#...#...#.#...................#.#...#.#...#.......#.#...#.#...#.#.#.....#.#...#...#...#.#.............#.....#...........#.........#...#.#.#.....#.......#.......#.....#...#.#...#...#
|
||||||
|
###.#.#.#.#.#.#.###.#.###.#####.#.#####.#.#.###.#.#.#.#####.#.###.#####.#.#.#.#.#.#.#.###.#.#.#####.###.#.###.#####.#.#.#.#####.###.#.#.#.#.#.#######.###.#.#.#########.###.#.#######.#
|
||||||
|
#.....#...................#...#.#...#...#...........#.#.......#.#.....#.#.....#...........#.....#.........#.#...........#...#.....#...#.....#.............#.#.#.........#.#.#.#.......#
|
||||||
|
#.#.#.#.#.###.#.#.#.#.#########.###.#.#.#.#########.#.#.#.#.#.#.###########.#.#.###.###.###.#.#.#.#.#.###.#.###.#.###.#.###.###.#.###.#.#.#####.#.#.#######.#.#.###.###.#.#.#.###.#####
|
||||||
|
#...#.#.#....7#.#...#.#.........#.....#...#.#.......#.....#...........#.#...#.......#.#...#.#...#...........#...#.#...#.....#...#.#.#...........#...#.#...#...#4....#...#...#...#.....#
|
||||||
|
###.#.#.###.#####.#.#.#.#.#.#.#.#.#.#.###.#.###.#.###.#######.#.#.###.#.#####.#.#####.###.#.###.#.#.#.#.#####.###.#.###.#.#.#.#.###.#.#.###.#.#.###.#.###.#########.#.###.#.#.#.#.#.#.#
|
||||||
|
#.....#.#.#.......#.#.#.......#.#.#.............#...#.........#.......#.#.#...........#.....#.....#...#...#.#.....#...#.....#.#.#...#...#...#.....#...#...#.#...#...#.#.#.....#...#.#.#
|
||||||
|
#.###.#.#.#########.#.#.#.#######.#.#.#.#.###.###.#####.#.#.#.#.#.###.#.#.#.#.#####.#.#.###.#.#.#.#.###.###.###.#.#.#######.#.#.###.#####.#.#.###.#.#.#####.###.#.#.###.#.#########.###
|
||||||
|
#.......#...#...#...#...#.#...#...#.#.#...#.#...........#.........#...#.#.#.#.....#...#.....#.#.....#...#...#...#.#.#.#...#...#.#...#.....#...........#.#.........#.....#.#.#...#.....#
|
||||||
|
#####.###.#####.#.#####.#######.#.#.#.###.#.#######.###.#####.###.#.#.#.#.#.#####.#####.###.###.###.#.#.###.#.#.#.###.#.#.###.#.#.#####.#.###.#.#.#.###.#.#.#.#.#.###.###.#.#.###.#.#.#
|
||||||
|
#.....#.#.#.......#...#.#.#.....#.........#...#.....#6......#...#.#...........#.......#.............#...#...#...#...#.#...#...#...#.....#.........#.#...#.....#.#.#.......#...#.#.#...#
|
||||||
|
#######################################################################################################################################################################################
|
||||||
5
inputs/24_test
Normal file
5
inputs/24_test
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
###########
|
||||||
|
#0.1.....2#
|
||||||
|
#.#######.#
|
||||||
|
#4.......3#
|
||||||
|
###########
|
||||||
3
main.go
3
main.go
@ -21,6 +21,7 @@ import (
|
|||||||
"aoc2016/day19"
|
"aoc2016/day19"
|
||||||
"aoc2016/day20"
|
"aoc2016/day20"
|
||||||
"aoc2016/day23"
|
"aoc2016/day23"
|
||||||
|
"aoc2016/day24"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -61,7 +62,7 @@ func initDayFunctions() {
|
|||||||
// 21: {1: day21.Part1, 2: day21.Part2},
|
// 21: {1: day21.Part1, 2: day21.Part2},
|
||||||
// 22: {1: day22.Part1, 2: day22.Part2},
|
// 22: {1: day22.Part1, 2: day22.Part2},
|
||||||
23: {1: day23.Part1, 2: day23.Part2},
|
23: {1: day23.Part1, 2: day23.Part2},
|
||||||
// 24: {1: day24.Part1, 2: day24.Part2},
|
24: {1: day24.Part1, 2: day24.Part2},
|
||||||
// 25: {1: day25.Part1, 2: day25.Part2},
|
// 25: {1: day25.Part1, 2: day25.Part2},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user