From aeeab9885f4817be2a5e48383418c8afaf233b86 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Tue, 10 Dec 2019 09:17:50 -0600 Subject: [PATCH] 2019 Day 10 Complete --- 2019/day10/input | 41 ++++++++++++++++++++ 2019/day10/main.go | 90 +++++++++++++++++++++++++++++++++++++++++++ 2019/day10/testinput1 | 5 +++ 2019/day10/testinput2 | 10 +++++ 2019/day10/testinput3 | 10 +++++ 2019/day10/testinput4 | 10 +++++ 2019/day10/testinput5 | 20 ++++++++++ helpers/coordinate.go | 37 ++++++++++++++++++ helpers/helpers.go | 7 ++++ 9 files changed, 230 insertions(+) create mode 100644 2019/day10/input create mode 100644 2019/day10/main.go create mode 100644 2019/day10/testinput1 create mode 100644 2019/day10/testinput2 create mode 100644 2019/day10/testinput3 create mode 100644 2019/day10/testinput4 create mode 100644 2019/day10/testinput5 create mode 100644 helpers/coordinate.go diff --git a/2019/day10/input b/2019/day10/input new file mode 100644 index 0000000..e5deb23 --- /dev/null +++ b/2019/day10/input @@ -0,0 +1,41 @@ +.#....#.###.........#..##.###.#.....##... +...........##.......#.#...#...#..#....#.. +...#....##..##.......#..........###..#... +....#....####......#..#.#........#....... +...............##..#....#...##..#...#..#. +..#....#....#..#.....#.#......#..#...#... +.....#.#....#.#...##.........#...#....... +#...##.#.#...#.......#....#........#..... +....##........#....#..........#.......#.. +..##..........##.....#....#.........#.... +...#..##......#..#.#.#...#............... +..#.##.........#...#.#.....#........#.... +#.#.#.#......#.#...##...#.........##....# +.#....#..#.....#.#......##.##...#.......# +..#..##.....#..#.........#...##.....#..#. +##.#...#.#.#.#.#.#.........#..#...#.##... +.#.....#......##..#.#..#....#....#####... +........#...##...#.....#.......#....#.#.# +#......#..#..#.#.#....##..#......###..... +............#..#.#.#....#.....##..#...... +...#.#.....#..#.......#..#.#............# +.#.#.....#..##.....#..#..............#... +.#.#....##.....#......##..#...#......#... +.......#..........#.###....#.#...##.#.... +.....##.#..#.....#.#.#......#...##..#.#.. +.#....#...#.#.#.......##.#.........#.#... +##.........#............#.#......#....#.. +.#......#.............#.#......#......... +.......#...##........#...##......#....#.. +#..#.....#.#...##.#.#......##...#.#..#... +#....##...#.#........#..........##....... +..#.#.....#.....###.#..#.........#......# +......##.#...#.#..#..#.##..............#. +.......##.#..#.#.............#..#.#...... +...#....##.##..#..#..#.....#...##.#...... +#....#..#.#....#...###...#.#.......#..... +.#..#...#......##.#..#..#........#....#.. +..#.##.#...#......###.....#.#........##.. +#.##.###.........#...##.....#..#....#.#.. +..........#...#..##..#..##....#.........# +..#..#....###..........##..#...#...#..#.. diff --git a/2019/day10/main.go b/2019/day10/main.go new file mode 100644 index 0000000..ef13cbb --- /dev/null +++ b/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) + } + } + } + } +} diff --git a/2019/day10/testinput1 b/2019/day10/testinput1 new file mode 100644 index 0000000..737ae7f --- /dev/null +++ b/2019/day10/testinput1 @@ -0,0 +1,5 @@ +.#..# +..... +##### +....# +...## diff --git a/2019/day10/testinput2 b/2019/day10/testinput2 new file mode 100644 index 0000000..987698f --- /dev/null +++ b/2019/day10/testinput2 @@ -0,0 +1,10 @@ +......#.#. +#..#.#.... +..#######. +.#.#.###.. +.#..#..... +..#....#.# +#..#....#. +.##.#..### +##...#..#. +.#....#### diff --git a/2019/day10/testinput3 b/2019/day10/testinput3 new file mode 100644 index 0000000..e28e424 --- /dev/null +++ b/2019/day10/testinput3 @@ -0,0 +1,10 @@ +#.#...#.#. +.###....#. +.#....#... +##.#.#.#.# +....#.#.#. +.##..###.# +..#...##.. +..##....## +......#... +.####.###. diff --git a/2019/day10/testinput4 b/2019/day10/testinput4 new file mode 100644 index 0000000..af5b6e9 --- /dev/null +++ b/2019/day10/testinput4 @@ -0,0 +1,10 @@ +.#..#..### +####.###.# +....###.#. +..###.##.# +##.##.#.#. +....###..# +..#.#..#.# +#..#.#.### +.##...##.# +.....#.#.. diff --git a/2019/day10/testinput5 b/2019/day10/testinput5 new file mode 100644 index 0000000..33437ba --- /dev/null +++ b/2019/day10/testinput5 @@ -0,0 +1,20 @@ +.#..##.###...####### +##.############..##. +.#.######.########.# +.###.#######.####.#. +#####.##.#.##.###.## +..#####..#.######### +#################### +#.####....###.#.#.## +##.################# +#####.##.###..####.. +..######..##.####### +####.##.####...##..# +.#####..#.######.### +##...#.##########... +#.##########.####### +.####.#.###.###.#.## +....##.##.###..##### +.#.#.###########.### +#.#.#.#####.####.### +###.##.####.##.#..## diff --git a/helpers/coordinate.go b/helpers/coordinate.go new file mode 100644 index 0000000..d89d49c --- /dev/null +++ b/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) +} diff --git a/helpers/helpers.go b/helpers/helpers.go index 426c933..6369216 100644 --- a/helpers/helpers.go +++ b/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+"=") {