2019 Day 10 Complete

This commit is contained in:
2019-12-10 09:17:50 -06:00
parent 1a8a0d3099
commit aeeab9885f
9 changed files with 230 additions and 0 deletions

41
2019/day10/input Normal file
View File

@@ -0,0 +1,41 @@
.#....#.###.........#..##.###.#.....##...
...........##.......#.#...#...#..#....#..
...#....##..##.......#..........###..#...
....#....####......#..#.#........#.......
...............##..#....#...##..#...#..#.
..#....#....#..#.....#.#......#..#...#...
.....#.#....#.#...##.........#...#.......
#...##.#.#...#.......#....#........#.....
....##........#....#..........#.......#..
..##..........##.....#....#.........#....
...#..##......#..#.#.#...#...............
..#.##.........#...#.#.....#........#....
#.#.#.#......#.#...##...#.........##....#
.#....#..#.....#.#......##.##...#.......#
..#..##.....#..#.........#...##.....#..#.
##.#...#.#.#.#.#.#.........#..#...#.##...
.#.....#......##..#.#..#....#....#####...
........#...##...#.....#.......#....#.#.#
#......#..#..#.#.#....##..#......###.....
............#..#.#.#....#.....##..#......
...#.#.....#..#.......#..#.#............#
.#.#.....#..##.....#..#..............#...
.#.#....##.....#......##..#...#......#...
.......#..........#.###....#.#...##.#....
.....##.#..#.....#.#.#......#...##..#.#..
.#....#...#.#.#.......##.#.........#.#...
##.........#............#.#......#....#..
.#......#.............#.#......#.........
.......#...##........#...##......#....#..
#..#.....#.#...##.#.#......##...#.#..#...
#....##...#.#........#..........##.......
..#.#.....#.....###.#..#.........#......#
......##.#...#.#..#..#.##..............#.
.......##.#..#.#.............#..#.#......
...#....##.##..#..#..#.....#...##.#......
#....#..#.#....#...###...#.#.......#.....
.#..#...#......##.#..#..#........#....#..
..#.##.#...#......###.....#.#........##..
#.##.###.........#...##.....#..#....#.#..
..........#...#..##..#..##....#.........#
..#..#....###..........##..#...#...#..#..

90
2019/day10/main.go Normal file
View File

@@ -0,0 +1,90 @@
package main
import (
"fmt"
"sort"
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
)
const MaxInt = int(^uint(0) >> 1)
var field []helpers.Coordinate
func main() {
inp := helpers.StdinToStringSlice()
initField(inp)
solve()
}
func initField(inp []string) {
for y, yb := range inp {
for x, xb := range yb {
if xb != '#' {
continue
}
field = append(field, helpers.Coordinate{X: x, Y: y})
}
}
}
func solve() {
// Figure out the best place to put the station
var bestCount int
var best helpers.Coordinate
var bestAngles map[float64][]helpers.Coordinate
for _, wrk := range field {
angles := make(map[float64][]helpers.Coordinate)
var count int
for _, tgt := range field {
if wrk == tgt {
continue
}
a := wrk.Angle(tgt)
if _, ok := angles[a]; !ok {
count++
}
angles[a] = append(angles[a], tgt)
}
if count > bestCount {
bestCount = count
best = wrk
bestAngles = angles
}
}
fmt.Println("Part 1:", best, bestCount)
var angleSlice []float64
for k := range bestAngles {
angleSlice = append(angleSlice, k)
}
sort.Float64s(angleSlice)
// Now start vaporizing
var hitCount int
var c200 helpers.Coordinate
for k := range bestAngles {
sort.Slice(bestAngles[k], func(i, j int) bool {
return bestAngles[k][i].Distance(best) < bestAngles[k][j].Distance(best)
})
}
for len(bestAngles) > 0 {
for _, v := range angleSlice {
if roids, ok := bestAngles[v]; ok {
// Destroy the first asteroid in the list
hitCount++
if hitCount == 200 {
c200 = roids[0]
res := (c200.X*100 + c200.Y)
fmt.Println("Part 2:", c200, res)
return
}
if len(roids) > 1 {
bestAngles[v] = roids[1:]
} else {
delete(bestAngles, v)
}
}
}
}
}

5
2019/day10/testinput1 Normal file
View File

@@ -0,0 +1,5 @@
.#..#
.....
#####
....#
...##

10
2019/day10/testinput2 Normal file
View File

@@ -0,0 +1,10 @@
......#.#.
#..#.#....
..#######.
.#.#.###..
.#..#.....
..#....#.#
#..#....#.
.##.#..###
##...#..#.
.#....####

10
2019/day10/testinput3 Normal file
View File

@@ -0,0 +1,10 @@
#.#...#.#.
.###....#.
.#....#...
##.#.#.#.#
....#.#.#.
.##..###.#
..#...##..
..##....##
......#...
.####.###.

10
2019/day10/testinput4 Normal file
View File

@@ -0,0 +1,10 @@
.#..#..###
####.###.#
....###.#.
..###.##.#
##.##.#.#.
....###..#
..#.#..#.#
#..#.#.###
.##...##.#
.....#.#..

20
2019/day10/testinput5 Normal file
View File

@@ -0,0 +1,20 @@
.#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##