Browse Source

2019 Day 10 Complete

master
Brian Buller 2 years ago
parent
commit
aeeab9885f
  1. 41
      2019/day10/input
  2. 90
      2019/day10/main.go
  3. 5
      2019/day10/testinput1
  4. 10
      2019/day10/testinput2
  5. 10
      2019/day10/testinput3
  6. 10
      2019/day10/testinput4
  7. 20
      2019/day10/testinput5
  8. 37
      helpers/coordinate.go
  9. 7
      helpers/helpers.go

41
2019/day10/input

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

90
2019/day10/main.go

@ -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

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

10
2019/day10/testinput2

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

10
2019/day10/testinput3

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

10
2019/day10/testinput4

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

20
2019/day10/testinput5

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

37
helpers/coordinate.go

@ -0,0 +1,37 @@
package aoc
import (
"fmt"
"math"
"strings"
)
type Coordinate struct {
X, Y int
}
func CoordinateFromString(str string) *Coordinate {
c := Coordinate{}
r := strings.NewReader(str)
_, err := fmt.Fscanf(r, "[%d, %d]", c.X, c.Y)
if err != nil {
panic(err)
}
return &c
}
func (c Coordinate) Angle(t Coordinate) float64 {
ret := math.Atan2(float64(t.X-c.X), float64(c.Y-t.Y)) * 180 / math.Pi
if ret < 0 {
ret = ret + 360
}
return ret
}
func (c Coordinate) String() string {
return fmt.Sprintf("[%d, %d]", c.X, c.Y)
}
func (c Coordinate) Distance(t Coordinate) int {
return AbsInt(c.X-t.X) + AbsInt(c.Y-t.Y)
}

7
helpers/helpers.go

@ -27,6 +27,13 @@ const (
MIN_INT = -MAX_INT - 1
)
func AbsInt(i int) int {
if i < 0 {
return i * -1
}
return i
}
func ArgIsSet(a string) bool {
for i := range os.Args {
if os.Args[i] == a || strings.HasPrefix(os.Args[i], a+"=") {

Loading…
Cancel
Save