2017 Day 03 Complete!

This commit is contained in:
Brian Buller 2017-12-03 14:17:56 -06:00
parent dc2725defe
commit e075f2bc44
3 changed files with 221 additions and 0 deletions

1
2017/day03/input Normal file
View File

@ -0,0 +1 @@
312051

129
2017/day03/main.go Normal file
View File

@ -0,0 +1,129 @@
package main
import (
"fmt"
"log"
"math"
"os"
"strconv"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: day03 <target>")
os.Exit(0)
}
target := Atoi(os.Args[1])
fmt.Println("== Part 1 ==")
part1(target)
fmt.Println("== Part 2 ==")
part2(target)
}
func part1(target int) {
idx := 1
lyrSz := 1
sizeSquare := int(math.Pow(float64(lyrSz), 2))
for sizeSquare < target {
lyrSz += 2
idx += 1
sizeSquare = int(math.Pow(float64(lyrSz), 2))
}
diff := sizeSquare - target
lyrJmp := lyrSz - 1
// Find the side it's on
side := int(diff / lyrJmp)
perps := make([]int, 4)
perps[0] = sizeSquare - (lyrJmp / 2)
perps[1] = perps[0] - lyrJmp
perps[2] = perps[1] - lyrJmp
perps[3] = perps[2] - lyrJmp
fmt.Println("Distance: ", int(math.Abs(float64(perps[side]-target)))+(idx-1))
}
const (
DIR_E = iota
DIR_N
DIR_W
DIR_S
DIR_ERROR
)
var memMap map[string]int
var lastX, lastY int
var lastDir int
func part2(target int) {
lastDir = DIR_S
lastX, lastY = 0, 0
val := 1
memMap = make(map[string]int)
memMap[getMapKey(lastX, lastY)] = val
for val <= target {
// Find the coordinate for the next one
lastX, lastY, lastDir = findNextMapPos()
val = getAdjacentSum(lastX, lastY)
memMap[getMapKey(lastX, lastY)] = val
}
fmt.Println("Result:", val, "at", getMapKey(lastX, lastY))
}
func getAdjacentSum(posX, posY int) int {
res := 0
for _, x := range []int{-1, 0, 1} {
for _, y := range []int{-1, 0, 1} {
if x == 0 && y == 0 {
continue
}
if i, ok := memMap[getMapKey((posX+x), (posY+y))]; ok {
res += i
}
}
}
return res
}
func findNextMapPos() (int, int, int) {
// First check if the current pos is empty
if _, ok := memMap[getMapKey(lastX, lastY)]; !ok {
return lastX, lastY, lastDir
}
// Check if the wrapping direction is free
nextDir := (lastDir + 1) % DIR_ERROR
testX, testY := getXYInDir(nextDir)
if _, ok := memMap[getMapKey(testX, testY)]; ok {
testX, testY = getXYInDir(lastDir)
nextDir = lastDir
}
// Otherwise continue in lastDir
return testX, testY, nextDir
}
func getXYInDir(dir int) (int, int) {
testX, testY := lastX, lastY
switch dir {
case DIR_E:
testX++
case DIR_N:
testY--
case DIR_W:
testX--
case DIR_S:
testY++
}
return testX, testY
}
func getMapKey(xPos, yPos int) string {
return fmt.Sprintf("%d;%d", xPos, yPos)
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}

91
2017/day03/problem Normal file
View File

@ -0,0 +1,91 @@
Advent of Code
--- Day 3: Spiral Memory ---
You come across an experimental new kind of memory stored on an infinite
two-dimensional grid.
Each square on the grid is allocated in a spiral pattern starting at a
location marked 1 and then counting up while spiraling outward. For example,
the first few squares are allocated like this:
17 16 15 14 13
18 5 4 3 12
19 6 1 2 11
20 7 8 9 10
21 22 23---> ...
While this is very space-efficient (no squares are skipped), requested data
must be carried back to square 1 (the location of the only access port for
this memory system) by programs that can only move up, down, left, or right.
They always take the shortest path: the Manhattan Distance between the
location of the data and square 1.
For example:
* Data from square 1 is carried 0 steps, since it's at the access port.
* Data from square 12 is carried 3 steps, such as: down, left, left.
* Data from square 23 is carried only 2 steps: up twice.
* Data from square 1024 must be carried 31 steps.
How many steps are required to carry the data from the square identified in
your puzzle input all the way to the access port?
Your puzzle answer was ____.
--- Part Two ---
As a stress test on the system, the programs here clear the grid and then
store the value 1 in square 1. Then, in the same allocation order as shown
above, they store the sum of the values in all adjacent squares, including
diagonals.
So, the first few squares' values are chosen as follows:
* Square 1 starts with the value 1.
* Square 2 has only one adjacent filled square (with value 1), so it also
stores 1.
* Square 3 has both of the above squares as neighbors and stores the sum of
their values, 2.
* Square 4 has all three of the aforementioned squares as neighbors and
stores the sum of their values, 4.
* Square 5 only has the first and fourth squares as neighbors, so it gets
the value 5.
Once a square is written, its value does not change. Therefore, the first few
squares would receive the following values:
147 142 133 122 59
304 5 4 2 57
330 10 1 1 54
351 11 23 25 26
362 747 806---> ...
What is the first value written that is larger than your puzzle input?
Your puzzle answer was _______.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your advent calendar and try another
puzzle.
Your puzzle input was 312051.
References
Visible links
. http://adventofcode.com/
. http://adventofcode.com/2017/about
. http://adventofcode.com/2017/support
. http://adventofcode.com/2017/events
. http://adventofcode.com/2017/settings
. http://adventofcode.com/2017/auth/logout
. http://adventofcode.com/2017
. http://adventofcode.com/2017
. http://adventofcode.com/2017/leaderboard
. http://adventofcode.com/2017/stats
. http://adventofcode.com/2017/sponsors
. http://adventofcode.com/2017/sponsors
. https://en.wikipedia.org/wiki/Taxicab_geometry
. http://adventofcode.com/2017