day13p1
This commit is contained in:
parent
6dc56b38da
commit
9bfc7ee244
114
day13/day.go
Normal file
114
day13/day.go
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
package day13
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
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, path tools.GridToggle) {
|
||||||
|
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 path.State(x, y) {
|
||||||
|
fmt.Print("O")
|
||||||
|
} else if maze.State(x, y) {
|
||||||
|
fmt.Print("#")
|
||||||
|
} else {
|
||||||
|
fmt.Print(".")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func shortestPathToTarget(maze, visited tools.GridToggle, startX, startY int) tools.GridToggle {
|
||||||
|
minSteps := fullGrid.GetOnCount()
|
||||||
|
var newVisited tools.GridToggle
|
||||||
|
visited.Set(startX, startY, tools.On)
|
||||||
|
origVisited := visited.Copy()
|
||||||
|
|
||||||
|
if startX == targetX && startY == targetY {
|
||||||
|
return visited
|
||||||
|
}
|
||||||
|
|
||||||
|
toCheckList := []tools.Coordinate{
|
||||||
|
{startX - 1, startY},
|
||||||
|
{startX + 1, startY},
|
||||||
|
{startX, startY - 1},
|
||||||
|
{startX, startY + 1},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, toCheck := range toCheckList {
|
||||||
|
if toCheck.X < 0 || toCheck.Y < 0 || toCheck.X > maze.MaxX || toCheck.Y > maze.MaxY {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !maze.State(toCheck.X, toCheck.Y) && !visited.State(toCheck.X, toCheck.Y) {
|
||||||
|
newVisited = shortestPathToTarget(maze, origVisited.Copy(), toCheck.X, toCheck.Y)
|
||||||
|
if newVisited.State(targetX, targetY) && newVisited.GetOnCount() < minSteps {
|
||||||
|
minSteps = newVisited.GetOnCount()
|
||||||
|
visited = newVisited
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if minSteps < fullGrid.GetOnCount() {
|
||||||
|
return visited
|
||||||
|
} else {
|
||||||
|
return fullGrid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
||||||
|
favNumber, targetX, targetY = parseInput(puzzle.GetInputArray())
|
||||||
|
maze := buildMaze(targetX+mazeHangover, targetY+mazeHangover)
|
||||||
|
|
||||||
|
path := shortestPathToTarget(maze, tools.NewGridToggle(), 1, 1)
|
||||||
|
printMaze(maze, path)
|
||||||
|
|
||||||
|
return path.GetOnCount() - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
2
inputs/13_test
Normal file
2
inputs/13_test
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
10
|
||||||
|
7,4
|
||||||
3
main.go
3
main.go
@ -12,6 +12,7 @@ import (
|
|||||||
"aoc2016/day09"
|
"aoc2016/day09"
|
||||||
"aoc2016/day10"
|
"aoc2016/day10"
|
||||||
"aoc2016/day12"
|
"aoc2016/day12"
|
||||||
|
"aoc2016/day13"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -41,7 +42,7 @@ func initDayFunctions() {
|
|||||||
10: {1: day10.Part1, 2: day10.Part2},
|
10: {1: day10.Part1, 2: day10.Part2},
|
||||||
// 11: {1: day11.Part1, 2: day11.Part2},
|
// 11: {1: day11.Part1, 2: day11.Part2},
|
||||||
12: {1: day12.Part1, 2: day12.Part2},
|
12: {1: day12.Part1, 2: day12.Part2},
|
||||||
// 13: {1: day13.Part1, 2: day13.Part2},
|
13: {1: day13.Part1, 2: day13.Part2},
|
||||||
// 14: {1: day14.Part1, 2: day14.Part2},
|
// 14: {1: day14.Part1, 2: day14.Part2},
|
||||||
// 15: {1: day15.Part1, 2: day15.Part2},
|
// 15: {1: day15.Part1, 2: day15.Part2},
|
||||||
// 16: {1: day16.Part1, 2: day16.Part2},
|
// 16: {1: day16.Part1, 2: day16.Part2},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user