aoc2016/day13/day.go
2021-10-22 07:44:18 +02:00

99 lines
2.2 KiB
Go

package day13
import (
"strconv"
"strings"
"tools"
)
type mazeNode struct {
x, y, nodeType int // ß = wall, 1 = path, 2 = start, 3 = target
neighbours []mazeNode
}
const mazeHangover int = 10
var favNumber, targetX, targetY int
var fullGrid = tools.NewGridToggle()
func parseInput(input []string) (favNumber, targetX, targetY int) {
favNumber, _ = strconv.Atoi(input[0])
parts := strings.Split(input[1], ",")
targetX, _ = strconv.Atoi(parts[0])
targetY, _ = strconv.Atoi(parts[1])
return favNumber, targetX, targetY
}
func buildMaze(maxX, maxY int) tools.GridToggle {
maze := tools.NewGridToggle()
for x := 0; x <= maxX; x++ {
for y := 0; y <= maxY; y++ {
fullGrid.Set(x, y, tools.On)
theNumber := int64(x*x + 3*x + 2*x*y + y + y*y + favNumber)
binNumber := strconv.FormatInt(theNumber, 2)
ones := 0
for _, c := range binNumber {
if c == '1' {
ones++
}
}
if ones%2 != 0 {
maze.Set(x, y, tools.On)
}
}
}
return maze
}
/*
func printMaze(maze tools.GridToggle, path []tools.Coordinate) {
pathMap := make(map[tools.Coordinate]int)
for i, c := range path {
pathMap[c] = i
}
for y := 0; y <= maze.MaxY; y++ {
for x := 0; x <= maze.MaxX; x++ {
if (x == targetX && y == targetY) || (x == 1 && y == 1) {
fmt.Print("X")
} else if _, ok := pathMap[tools.Coordinate{X: x, Y: y}]; ok {
fmt.Print("O")
} else if !maze.State(x, y) {
fmt.Print(".")
} else {
fmt.Print("#")
}
}
fmt.Println()
}
}
*/
func Part1(puzzle tools.AoCPuzzle) interface{} {
favNumber, targetX, targetY = parseInput(puzzle.GetInputArray())
maze := buildMaze(targetX+mazeHangover, targetY+mazeHangover)
path := maze.GetPathAStar(tools.Coordinate{X: 1, Y: 1}, tools.Coordinate{X: targetX, Y: targetY}, false)
return len(path) - 1
}
func Part2(puzzle tools.AoCPuzzle) interface{} {
favNumber, targetX, targetY = parseInput(puzzle.GetInputArray())
maze := buildMaze(52, 52)
targets := 0
for x := 0; x < 50; x++ {
for y := 0; y < 50; y++ {
if maze.State(x, y) {
continue
}
checkPath := maze.GetPathAStar(tools.Coordinate{X: 1, Y: 1}, tools.Coordinate{X: x, Y: y}, false)
if checkPath != nil && len(checkPath) <= 51 {
targets++
}
}
}
return targets
}