mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 20:53:30 +02:00
might be making a mistake starting 2018...
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
...###.#########.####
|
||||
.######.###.###.##...
|
||||
####.########.#####.#
|
||||
########.####.##.###.
|
||||
####..#.####.#.#.##..
|
||||
#.################.##
|
||||
..######.##.##.#####.
|
||||
#.####.#####.###.#.##
|
||||
#####.#########.#####
|
||||
#####.##..##..#.#####
|
||||
##.######....########
|
||||
.#######.#.#########.
|
||||
.#.##.#.#.#.##.###.##
|
||||
######...####.#.#.###
|
||||
###############.#.###
|
||||
#.#####.##..###.##.#.
|
||||
##..##..###.#.#######
|
||||
#..#..########.#.##..
|
||||
#.#.######.##.##...##
|
||||
.#.##.#####.#..#####.
|
||||
#.#.##########..#.##.
|
||||
@@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// need to read the input.txt file and split each line into a slice
|
||||
input := util.ReadFile("../input.txt")
|
||||
stringSlice := strings.Split(input, "\n")
|
||||
|
||||
// split into a 2D grid with each character
|
||||
gridSlice := make([][]string, len(stringSlice))
|
||||
for i, str := range stringSlice {
|
||||
gridSlice[i] = strings.Split(str, "")
|
||||
}
|
||||
|
||||
// will be the final result value
|
||||
var result int
|
||||
var finalCoords [2]int
|
||||
// iterate through entire slice, for each asteroid found, call a helper function
|
||||
for rowIndex, rowSlice := range gridSlice {
|
||||
for colIndex, element := range rowSlice {
|
||||
if element == "#" {
|
||||
// # are "asteroids", . are empty space
|
||||
// helper function will return how many asteroids are "findable" from the current asteroid
|
||||
visibleFromElement := visibleFromAsteroid(gridSlice, rowIndex, colIndex)
|
||||
|
||||
// take max of return of helper function at end of each loop
|
||||
if result < visibleFromElement {
|
||||
result = visibleFromElement
|
||||
finalCoords[0], finalCoords[1] = rowIndex, colIndex
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// print out the max found
|
||||
fmt.Printf("best asteroid for the station: row[%v] col[%v]\n", finalCoords[0], finalCoords[1]) // [13, 11]
|
||||
fmt.Println("from 13, 11 (y, x)", result)
|
||||
}
|
||||
|
||||
// helper function will take, x and y coordinates, and the 2D slice
|
||||
// will create a two maps of floats to booleans
|
||||
// (one map to cover left side of asteroid, one map to cover right side of asteroid)
|
||||
// so that anything that is blocked will not be double counted
|
||||
// and edge case handling for planets vertically above or below the current asteroid
|
||||
func visibleFromAsteroid(grid [][]string, row, col int) (result int) {
|
||||
// make the two maps
|
||||
leftMap, rightMap := make(map[float64]bool), make(map[float64]bool)
|
||||
// make the two booleans for up and down. zero value is false
|
||||
var upBool, downBool bool
|
||||
|
||||
// iterate through every element of the grid slices
|
||||
for rowIndex, rowSlice := range grid {
|
||||
for colIndex, element := range rowSlice {
|
||||
// NOTE this control flow is _GROSS_. Better solution in part2 solution
|
||||
// ensure element is an asteroid & not the asteroid that the helper function is being run on
|
||||
if element == "#" && !(row == rowIndex && col == colIndex) {
|
||||
rise := rowIndex - row
|
||||
run := colIndex - col
|
||||
|
||||
// handle if the found asteroid is directly above the inputted row/col asteroid
|
||||
if run == 0 {
|
||||
if rise < 0 {
|
||||
// check down
|
||||
// note that up and down are semantically "flipped" due to the 2 row being "above" the 0 row
|
||||
if !downBool {
|
||||
downBool = true
|
||||
result++
|
||||
}
|
||||
} else {
|
||||
if !upBool {
|
||||
upBool = true
|
||||
result++
|
||||
}
|
||||
}
|
||||
} else {
|
||||
slope := float64(rise) / float64(run)
|
||||
// handle left or right map
|
||||
if run < 0 {
|
||||
// leftMap
|
||||
if _, inLeftMap := leftMap[slope]; !inLeftMap {
|
||||
leftMap[slope] = true
|
||||
result++
|
||||
}
|
||||
} else {
|
||||
// rightMap
|
||||
if _, inRightMap := rightMap[slope]; !inRightMap {
|
||||
rightMap[slope] = true
|
||||
result++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"adventofcode/day10/part2/trig"
|
||||
"adventofcode/util"
|
||||
)
|
||||
|
||||
/*
|
||||
Overall approach:
|
||||
- need to make a map of some kind
|
||||
make it a slice where each element is a struct
|
||||
- each struct will contain:
|
||||
x
|
||||
y
|
||||
degOffVert float64 (degrees 0 -> 360)
|
||||
distance float64
|
||||
- iterate through the slice of structs
|
||||
- store the index of the minimum distance
|
||||
|
||||
- remove it from the slice of structs
|
||||
- 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
|
||||
type Asteroid struct {
|
||||
x int
|
||||
y int
|
||||
degOffVert float64
|
||||
distance float64
|
||||
}
|
||||
|
||||
func main() {
|
||||
// read input.txt file, split it into a slice of lines
|
||||
input := util.ReadFile("../input.txt")
|
||||
stringSlice := strings.Split(input, "\n")
|
||||
|
||||
// generate 2D grid of each character from stringSlice
|
||||
inputGrid := make([][]string, len(stringSlice))
|
||||
for i, str := range stringSlice {
|
||||
inputGrid[i] = strings.Split(str, "")
|
||||
}
|
||||
|
||||
allAsteroids := makeAsteroidsSlice(inputGrid)
|
||||
|
||||
// need to start this just to the left of zero to get that as the first input
|
||||
lastDegreeUsed := 359.999999
|
||||
// to store the last vaporized asteroid to output its coordinates
|
||||
var lastAsteroid Asteroid
|
||||
|
||||
for i := 0; i < 200; i++ {
|
||||
// iterate through all of allAsteroids and find the next closest degree
|
||||
var indexOfAsteroidToDelete int // will be updated by iMin
|
||||
|
||||
// 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!
|
||||
|
||||
// iterate over the entire slice of asteroids
|
||||
for iMin, eAsteroid := range allAsteroids {
|
||||
// calculate the degrees difference
|
||||
degDiff := eAsteroid.degOffVert - lastDegreeUsed
|
||||
if degDiff <= 0 { // account for the diff passing over zero
|
||||
degDiff += 360
|
||||
}
|
||||
// if this asteroid has a smaller degrees difference, update all min values
|
||||
if degDiff < minDegDiff {
|
||||
minDist = eAsteroid.distance
|
||||
minDegDiff = degDiff
|
||||
indexOfAsteroidToDelete = iMin
|
||||
} else if degDiff == minDegDiff && minDist > eAsteroid.distance {
|
||||
// OR if the degDiff is the same but the distance to 13,11 is smaller
|
||||
// update just the minDistance and index for this asteroid
|
||||
minDist = eAsteroid.distance
|
||||
indexOfAsteroidToDelete = iMin
|
||||
}
|
||||
}
|
||||
|
||||
// remove the element at index indexOfAst
|
||||
// this doesn't maintain order, but I don't care about order right now
|
||||
lastAsteroid = allAsteroids[indexOfAsteroidToDelete]
|
||||
|
||||
// swap last element to indexToDelete
|
||||
allAsteroids[indexOfAsteroidToDelete] = allAsteroids[len(allAsteroids)-1]
|
||||
// re-size slice to effectively pop last element off of slice
|
||||
allAsteroids = allAsteroids[:len(allAsteroids)-1]
|
||||
|
||||
// update last deg used by adding the diff to it
|
||||
lastDegreeUsed += minDegDiff
|
||||
if lastDegreeUsed >= 360 {
|
||||
// if we pass over 360, subtract 360
|
||||
lastDegreeUsed -= 360
|
||||
}
|
||||
}
|
||||
|
||||
// print the last used asteroid
|
||||
fmt.Println("Last asteroid", lastAsteroid)
|
||||
// print the AoC-formatted answer
|
||||
fmt.Println("Advent of code answer: ", lastAsteroid.y*100+lastAsteroid.x)
|
||||
}
|
||||
|
||||
func makeAsteroidsSlice(grid [][]string) []Asteroid {
|
||||
result := make([]Asteroid, 0)
|
||||
|
||||
// iterate through the entire grid
|
||||
for rowIndex, rowSlice := range grid {
|
||||
for colIndex, element := range rowSlice {
|
||||
// if an asteroid is found...
|
||||
if element == "#" && !(rowIndex == 13 && colIndex == 11) {
|
||||
// calculate the degree and dist
|
||||
// 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: trig.AngleOffVertical(13, 11, rowIndex, colIndex),
|
||||
distance: trig.Distance(13, 11, rowIndex, colIndex),
|
||||
}
|
||||
result = append(result, ast)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package trig
|
||||
|
||||
import "math"
|
||||
|
||||
/*
|
||||
AngleOffVertical takes in two 2D points, it calculates the angle
|
||||
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)
|
||||
|
||||
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
|
||||
func Distance(startX, startY, endX, endY int) float64 {
|
||||
dx := startX - endX
|
||||
dy := startY - endY
|
||||
return math.Sqrt(float64(dx*dx) + float64(dy*dy))
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
--- Day 10: Monitoring Station ---
|
||||
You fly into the asteroid belt and reach the Ceres monitoring station. The Elves here have an emergency: they're having trouble tracking all of the asteroids and can't be sure they're safe.
|
||||
|
||||
The Elves would like to build a new monitoring station in a nearby area of space; they hand you a map of all of the asteroids in that region (your puzzle input).
|
||||
|
||||
The map indicates whether each position is empty (.) or contains an asteroid (#). The asteroids are much smaller than they appear on the map, and every asteroid is exactly in the center of its marked position. The asteroids can be described with X,Y coordinates where X is the distance from the left edge and Y is the distance from the top edge (so the top-left corner is 0,0 and the position immediately to its right is 1,0).
|
||||
|
||||
Your job is to figure out which asteroid would be the best place to build a new monitoring station. A monitoring station can detect any asteroid to which it has direct line of sight - that is, there cannot be another asteroid exactly between them. This line of sight can be at any angle, not just lines aligned to the grid or diagonally. The best location is the asteroid that can detect the largest number of other asteroids.
|
||||
|
||||
For example, consider the following map:
|
||||
|
||||
.#..#
|
||||
.....
|
||||
#####
|
||||
....#
|
||||
...##
|
||||
The best location for a new monitoring station on this map is the highlighted asteroid at 3,4 because it can detect 8 asteroids, more than any other location. (The only asteroid it cannot detect is the one at 1,0; its view of this asteroid is blocked by the asteroid at 2,2.) All other asteroids are worse locations; they can detect 7 or fewer other asteroids. Here is the number of other asteroids a monitoring station on each asteroid could detect:
|
||||
|
||||
.7..7
|
||||
.....
|
||||
67775
|
||||
....7
|
||||
...87
|
||||
Here is an asteroid (#) and some examples of the ways its line of sight might be blocked. If there were another asteroid at the location of a capital letter, the locations marked with the corresponding lowercase letter would be blocked and could not be detected:
|
||||
|
||||
#.........
|
||||
...A......
|
||||
...B..a...
|
||||
.EDCG....a
|
||||
..F.c.b...
|
||||
.....c....
|
||||
..efd.c.gb
|
||||
.......c..
|
||||
....f...c.
|
||||
...e..d..c
|
||||
Here are some larger examples:
|
||||
|
||||
Best is 5,8 with 33 other asteroids detected:
|
||||
|
||||
......#.#.
|
||||
#..#.#....
|
||||
..#######.
|
||||
.#.#.###..
|
||||
.#..#.....
|
||||
..#....#.#
|
||||
#..#....#.
|
||||
.##.#..###
|
||||
##...#..#.
|
||||
.#....####
|
||||
Best is 1,2 with 35 other asteroids detected:
|
||||
|
||||
#.#...#.#.
|
||||
.###....#.
|
||||
.#....#...
|
||||
##.#.#.#.#
|
||||
....#.#.#.
|
||||
.##..###.#
|
||||
..#...##..
|
||||
..##....##
|
||||
......#...
|
||||
.####.###.
|
||||
Best is 6,3 with 41 other asteroids detected:
|
||||
|
||||
.#..#..###
|
||||
####.###.#
|
||||
....###.#.
|
||||
..###.##.#
|
||||
##.##.#.#.
|
||||
....###..#
|
||||
..#.#..#.#
|
||||
#..#.#.###
|
||||
.##...##.#
|
||||
.....#.#..
|
||||
Best is 11,13 with 210 other asteroids detected:
|
||||
|
||||
.#..##.###...#######
|
||||
##.############..##.
|
||||
.#.######.########.#
|
||||
.###.#######.####.#.
|
||||
#####.##.#.##.###.##
|
||||
..#####..#.#########
|
||||
####################
|
||||
#.####....###.#.#.##
|
||||
##.#################
|
||||
#####.##.###..####..
|
||||
..######..##.#######
|
||||
####.##.####...##..#
|
||||
.#####..#.######.###
|
||||
##...#.##########...
|
||||
#.##########.#######
|
||||
.####.#.###.###.#.##
|
||||
....##.##.###..#####
|
||||
.#.#.###########.###
|
||||
#.#.#.#####.####.###
|
||||
###.##.####.##.#..##
|
||||
Find the best location for a new monitoring station. How many other asteroids can be detected from that location?
|
||||
|
||||
Your puzzle answer was 227.
|
||||
|
||||
--- Part Two ---
|
||||
Once you give them the coordinates, the Elves quickly deploy an Instant Monitoring Station to the location and discover the worst: there are simply too many asteroids.
|
||||
|
||||
The only solution is complete vaporization by giant laser.
|
||||
|
||||
Fortunately, in addition to an asteroid scanner, the new monitoring station also comes equipped with a giant rotating laser perfect for vaporizing asteroids. The laser starts by pointing up and always rotates clockwise, vaporizing any asteroid it hits.
|
||||
|
||||
If multiple asteroids are exactly in line with the station, the laser only has enough power to vaporize one of them before continuing its rotation. In other words, the same asteroids that can be detected can be vaporized, but if vaporizing one asteroid makes another one detectable, the newly-detected asteroid won't be vaporized until the laser has returned to the same position by rotating a full 360 degrees.
|
||||
|
||||
For example, consider the following map, where the asteroid with the new monitoring station (and laser) is marked X:
|
||||
|
||||
.#....#####...#..
|
||||
##...##.#####..##
|
||||
##...#...#.#####.
|
||||
..#.....X...###..
|
||||
..#.#.....#....##
|
||||
The first nine asteroids to get vaporized, in order, would be:
|
||||
|
||||
.#....###24...#..
|
||||
##...##.13#67..9#
|
||||
##...#...5.8####.
|
||||
..#.....X...###..
|
||||
..#.#.....#....##
|
||||
Note that some asteroids (the ones behind the asteroids marked 1, 5, and 7) won't have a chance to be vaporized until the next full rotation. The laser continues rotating; the next nine to be vaporized are:
|
||||
|
||||
.#....###.....#..
|
||||
##...##...#.....#
|
||||
##...#......1234.
|
||||
..#.....X...5##..
|
||||
..#.9.....8....76
|
||||
The next nine to be vaporized are then:
|
||||
|
||||
.8....###.....#..
|
||||
56...9#...#.....#
|
||||
34...7...........
|
||||
..2.....X....##..
|
||||
..1..............
|
||||
Finally, the laser completes its first full rotation (1 through 3), a second rotation (4 through 8), and vaporizes the last asteroid (9) partway through its third rotation:
|
||||
|
||||
......234.....6..
|
||||
......1...5.....7
|
||||
.................
|
||||
........X....89..
|
||||
.................
|
||||
In the large example above (the one with the best monitoring station location at 11,13):
|
||||
|
||||
The 1st asteroid to be vaporized is at 11,12.
|
||||
The 2nd asteroid to be vaporized is at 12,1.
|
||||
The 3rd asteroid to be vaporized is at 12,2.
|
||||
The 10th asteroid to be vaporized is at 12,8.
|
||||
The 20th asteroid to be vaporized is at 16,0.
|
||||
The 50th asteroid to be vaporized is at 16,9.
|
||||
The 100th asteroid to be vaporized is at 10,16.
|
||||
The 199th asteroid to be vaporized is at 9,6.
|
||||
The 200th asteroid to be vaporized is at 8,2.
|
||||
The 201st asteroid to be vaporized is at 10,9.
|
||||
The 299th and final asteroid to be vaporized is at 11,1.
|
||||
The Elves are placing bets on which will be the 200th asteroid to be vaporized. Win the bet by determining which asteroid that will be; what do you get if you multiply its X coordinate by 100 and then add its Y coordinate? (For example, 8,2 becomes 802.)
|
||||
|
||||
Your puzzle answer was 604.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
Reference in New Issue
Block a user