day10: some cleanup

This commit is contained in:
alexchao26
2020-08-03 20:04:23 -04:00
parent 40f4e54bc2
commit 936489e620
5 changed files with 50 additions and 135 deletions
+6 -20
View File
@@ -10,7 +10,7 @@ import (
)
/*
Overall approach...
Overall approach:
- need to make a map of some kind
make it a slice where each element is a struct
- each struct will contain:
@@ -36,8 +36,8 @@ type Asteroid struct {
func main() {
// read input.txt file, split it into a slice of lines
contents := util.ReadFile("../input.txt") // test/example case @ "./test.txt"
stringSlice := strings.Split(contents, "\n")
input := util.ReadFile("../input.txt")
stringSlice := strings.Split(input, "\n")
// generate 2D grid of each character from stringSlice
inputGrid := make([][]string, len(stringSlice))
@@ -45,18 +45,7 @@ func main() {
inputGrid[i] = strings.Split(str, "")
}
//* tests while building the TangetAndDistance function
// fmt.Println(trig.TangentAndDistance(13, 11, 0, 11)) // 0 13
// fmt.Println(trig.TangentAndDistance(13, 11, 15, 11)) // 180 2
// fmt.Println(trig.TangentAndDistance(13, 11, 13, 16)) // 90 5
// fmt.Println(trig.TangentAndDistance(13, 11, 13, 9)) // 270 2
// fmt.Println(trig.TangentAndDistance(1, 1, 0, 2)) // 45 some sqrt
// fmt.Println(trig.TangentAndDistance(1, 1, 2, 2)) // 135 some sqrt
// fmt.Println(trig.TangentAndDistance(1, 1, 2, 0)) // 225 some sqrt
// fmt.Println(trig.TangentAndDistance(1, 1, 0, 0)) // 315 some sqrt
allAsteroids := makeAsteroidsSlice(inputGrid)
// fmt.Println(allAsteroids)
// need to start this just to the left of zero to get that as the first input
lastDegreeUsed := 359.999999
@@ -105,9 +94,6 @@ func main() {
// if we pass over 360, subtract 360
lastDegreeUsed -= 360
}
// fmt.Println(minDegDiff)
// fmt.Println("lastAsteroid", i, lastAsteroid)
// fmt.Println(indexOfAsteroidToDelete, allAsteroids[indexOfAsteroidToDelete])
}
// print the last used asteroid
@@ -125,13 +111,13 @@ func makeAsteroidsSlice(grid [][]string) []Asteroid {
// if an asteroid is found...
if element == "#" && !(rowIndex == 13 && colIndex == 11) {
// calculate the degree and dist
degree, dist := trig.TangentAndDistance(13, 11, rowIndex, colIndex)
// degree, dist := trig.TangentAndDistance(13, 11, rowIndex, colIndex)
// create an instance of an Asteroid struct and append it to the result slice
ast := Asteroid{
x: rowIndex,
y: colIndex,
degOffVert: degree,
distance: dist,
degOffVert: trig.AngleOffVertical(13, 11, rowIndex, colIndex),
distance: trig.Distance(13, 11, rowIndex, colIndex),
}
result = append(result, ast)
}
-20
View File
@@ -1,20 +0,0 @@
.#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##
+29 -51
View File
@@ -1,62 +1,40 @@
package trig
import (
"math"
)
// TangentAndDistance docz
// startX will "always" be 13
// startY will "always" be 11
// 0 <= angleOffVery < 360
func TangentAndDistance(startX, startY, endX, endY int) (angleOffVert, distance float64) {
rise, run := float64(endX)-float64(startX), float64(endY)-float64(startY)
// fmt.Println(rise, run)
// edge cases for verticals
if run == 0 && rise < 0 {
return 0, -1 * rise // endXY is up
}
if run == 0 && rise > 0 {
return 180, rise // endXY is down
}
// handle left or right?
if rise == 0 && run < 0 {
return 270, -1 * run // left
}
if rise == 0 && run > 0 {
return 90, run // right
}
// not verticals
// calculate return distance
distance = rise*rise + run*run
distance = math.Sqrt(distance)
// calculate arctangent which will be in radians
// determine quadrent
if rise < 0 && run > 0 { // top right
angleOffVert = -1 * math.Atan(run/rise) * 180 / math.Pi
} else if rise > 0 && run > 0 { // bottom right
angleOffVert = 90 + math.Atan(rise/run)*180/math.Pi
} else if rise > 0 && run < 0 { // bottom left
angleOffVert = 180 + -1*math.Atan(run/rise)*180/math.Pi
} else if rise < 0 && run < 0 { // top left
angleOffVert = 270 + math.Atan(rise/run)*180/math.Pi
}
return angleOffVert, distance
}
import "math"
/*
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
between the line and a vertical line (straight up from origin)
NOTE: "up"/"top" and "down" are lexically flipped b/c of drawing a grid
where 0, 0 is the top left corner and higher numbers physically go DOWN
but lexically increase/go UP 🤦‍♂️
*/
func AngleOffVertical(startX, startY, endX, endY int) float64 {
rise := float64(endX) - float64(startX)
run := float64(endY) - float64(startY)
return 0
var angle float64
// basically a big if/elseif/else block
switch {
case run == 0 && rise < 0: // up
angle = 0
case run == 0 && rise > 0: // down
angle = 180
case rise == 0 && run < 0: // left
angle = 270
case rise == 0 && run > 0: // right
angle = 90
case rise < 0 && run > 0: // top right
angle = -1 * math.Atan(run/rise) * 180 / math.Pi
case rise > 0 && run > 0: // bottom right
angle = 90 + math.Atan(rise/run)*180/math.Pi
case rise > 0 && run < 0: // bottom left
angle = 180 + -1*math.Atan(run/rise)*180/math.Pi
case rise < 0 && run < 0: // top left
angle = 270 + math.Atan(rise/run)*180/math.Pi
}
return angle
}
// Distance calculates the distance between two sets of 2D coordinates via Pythagorean's theorem