day10 refactor, still needs a lot of work

This commit is contained in:
alexchao26
2020-08-01 22:40:03 -04:00
parent af05bc86c3
commit 18f80cb4f5
3 changed files with 39 additions and 23 deletions
+17 -18
View File
@@ -23,7 +23,9 @@ import (
- remove it from the slice of structs - remove it from the slice of structs
- if this is the 200th iteration, store the x and y to return at the end - if this is the 200th iteration, store the x and y to return at the end
- NOTE there are a limited number of asteroids because of the fixed size of the input, so having a O(200 * n) where n is the number of Asteroids, is not a _terrible_ time complexity
*/ */
// Asteroid data // Asteroid data
type Asteroid struct { type Asteroid struct {
x int x int
@@ -35,14 +37,12 @@ type Asteroid struct {
func main() { func main() {
// read input.txt file, split it into a slice of lines // read input.txt file, split it into a slice of lines
contents := util.ReadFile("../input.txt") // test/example case @ "./test.txt" contents := util.ReadFile("../input.txt") // test/example case @ "./test.txt"
// convert into a string slice
stringSlice := strings.Split(contents, "\n") stringSlice := strings.Split(contents, "\n")
// generate 2D grid of each character from stringSlice // generate 2D grid of each character from stringSlice
gridSlice := make([][]string, len(stringSlice)) inputGrid := make([][]string, len(stringSlice))
for i, str := range stringSlice { for i, str := range stringSlice {
gridSlice[i] = strings.Split(str, "") inputGrid[i] = strings.Split(str, "")
} }
//* tests while building the TangetAndDistance function //* tests while building the TangetAndDistance function
@@ -55,8 +55,8 @@ func main() {
// fmt.Println(trig.TangentAndDistance(1, 1, 2, 0)) // 225 some sqrt // fmt.Println(trig.TangentAndDistance(1, 1, 2, 0)) // 225 some sqrt
// fmt.Println(trig.TangentAndDistance(1, 1, 0, 0)) // 315 some sqrt // fmt.Println(trig.TangentAndDistance(1, 1, 0, 0)) // 315 some sqrt
sliceAll := fillSlice(gridSlice) allAsteroids := makeAsteroidsSlice(inputGrid)
// fmt.Println(sliceAll) // fmt.Println(allAsteroids)
// need to start this just to the left of zero to get that as the first input // need to start this just to the left of zero to get that as the first input
lastDegreeUsed := 359.999999 lastDegreeUsed := 359.999999
@@ -64,16 +64,14 @@ func main() {
var lastAsteroid Asteroid var lastAsteroid Asteroid
for i := 0; i < 200; i++ { for i := 0; i < 200; i++ {
// fmt.Println("---lastDegUsed", lastDegreeUsed) // this number should change in very small increments for each loop // iterate through all of allAsteroids and find the next closest degree
// iterate through all of sliceAll and find the next closest degree
var indexOfAsteroidToDelete int // will be updated by iMin var indexOfAsteroidToDelete int // will be updated by iMin
// reset the minDegDiff and minDist for each run of the outer loop // reset the minDegDiff and minDist for each run of the outer loop
minDegDiff, minDist := math.Inf(1), math.Inf(1) // I can use inf now b/c I'm using float64's! minDegDiff, minDist := math.Inf(1), math.Inf(1) // I can use inf now b/c I'm using float64's!
// iterate over the entire slice of asteroids // iterate over the entire slice of asteroids
for iMin, eAsteroid := range sliceAll { for iMin, eAsteroid := range allAsteroids {
// calculate the degrees difference // calculate the degrees difference
degDiff := eAsteroid.degOffVert - lastDegreeUsed degDiff := eAsteroid.degOffVert - lastDegreeUsed
if degDiff <= 0 { // account for the diff passing over zero if degDiff <= 0 { // account for the diff passing over zero
@@ -90,15 +88,16 @@ func main() {
minDist = eAsteroid.distance minDist = eAsteroid.distance
indexOfAsteroidToDelete = iMin indexOfAsteroidToDelete = iMin
} }
} }
// remove the element at index indexOfAst..
// this doesn't maintain order, but I don't care about order right now... // remove the element at index indexOfAst
lastAsteroid = sliceAll[indexOfAsteroidToDelete] // this doesn't maintain order, but I don't care about order right now
lastAsteroid = allAsteroids[indexOfAsteroidToDelete]
// swap last element to indexToDelete // swap last element to indexToDelete
sliceAll[indexOfAsteroidToDelete] = sliceAll[len(sliceAll)-1] allAsteroids[indexOfAsteroidToDelete] = allAsteroids[len(allAsteroids)-1]
// re-size slice to effectively pop last element off of slice // re-size slice to effectively pop last element off of slice
sliceAll = sliceAll[:len(sliceAll)-1] allAsteroids = allAsteroids[:len(allAsteroids)-1]
// update last deg used by adding the diff to it // update last deg used by adding the diff to it
lastDegreeUsed += minDegDiff lastDegreeUsed += minDegDiff
@@ -108,7 +107,7 @@ func main() {
} }
// fmt.Println(minDegDiff) // fmt.Println(minDegDiff)
// fmt.Println("lastAsteroid", i, lastAsteroid) // fmt.Println("lastAsteroid", i, lastAsteroid)
// fmt.Println(indexOfAsteroidToDelete, sliceAll[indexOfAsteroidToDelete]) // fmt.Println(indexOfAsteroidToDelete, allAsteroids[indexOfAsteroidToDelete])
} }
// print the last used asteroid // print the last used asteroid
@@ -117,7 +116,7 @@ func main() {
fmt.Println("Advent of code answer: ", lastAsteroid.y*100+lastAsteroid.x) fmt.Println("Advent of code answer: ", lastAsteroid.y*100+lastAsteroid.x)
} }
func fillSlice(grid [][]string) []Asteroid { func makeAsteroidsSlice(grid [][]string) []Asteroid {
result := make([]Asteroid, 0) result := make([]Asteroid, 0)
// iterate through the entire grid // iterate through the entire grid
+18
View File
@@ -47,3 +47,21 @@ func TangentAndDistance(startX, startY, endX, endY int) (angleOffVert, distance
return angleOffVert, distance return angleOffVert, distance
} }
/*
AngleOffVertical takes in two 2D points, it calculates the angle
between the line between then and a vertical line. The angle
returned is to the right of the vertical, i.e. from the vertical
and through the (mathematical) first quadrant
*/
func AngleOffVertical(startX, startY, endX, endY int) float64 {
return 0
}
// Distance calculates the distance between two sets of 2D coordinates via Pythagorean's theorem
func Distance(startX, startY, endX, endY int) float64 {
dx := startX - endX
dy := startY - endY
return math.Sqrt(float64(dx*dx) + float64(dy*dy))
}
+4 -5
View File
@@ -1,7 +1,6 @@
package util package util
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"path" "path"
@@ -9,8 +8,8 @@ import (
) )
/* /*
ReadFile takes the relative path from *the caller* and returns the contents ReadFile is a wrapper over io/ioutil.ReadFile but also determines the
of the file as a string dynamic absolute path to the file.
*/ */
func ReadFile(pathFromCaller string) string { func ReadFile(pathFromCaller string) string {
// Docs: https://golang.org/pkg/runtime/#Caller // Docs: https://golang.org/pkg/runtime/#Caller
@@ -20,10 +19,10 @@ func ReadFile(pathFromCaller string) string {
log.Fatal("Could not find Caller of util.ReadFile") log.Fatal("Could not find Caller of util.ReadFile")
} }
// parse directory with pathFromCaller (which could be relative to Directory)
absolutePath := path.Join(path.Dir(filename), pathFromCaller) absolutePath := path.Join(path.Dir(filename), pathFromCaller)
fmt.Println("abs path is", absolutePath) // read the entire file & return the byte slice as a string
content, err := ioutil.ReadFile(absolutePath) content, err := ioutil.ReadFile(absolutePath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)