Skip to content

Commit

Permalink
Implementation for 10a
Browse files Browse the repository at this point in the history
  • Loading branch information
nlowe committed Dec 10, 2019
1 parent 7bdcaf5 commit a13f9b8
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 0 deletions.
97 changes: 97 additions & 0 deletions challenge/day10/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package day10

import (
"fmt"
"math"

"github.com/nlowe/aoc2019/challenge"
"github.com/spf13/cobra"
)

const symAsteroid = '#'

var A = &cobra.Command{
Use: "10a",
Short: "Day 10, Problem A",
Run: func(_ *cobra.Command, _ []string) {
fmt.Printf("Answer: %d\n", a(challenge.FromFile()))
},
}

type asteroid struct {
x float64
y float64
}

func (a asteroid) distanceTo(other asteroid) float64 {
return math.Sqrt(math.Pow(a.x-other.x, 2) + math.Pow(a.y-other.y, 2))
}

func (a asteroid) slopeTo(other asteroid) float64 {
if a.y == other.y {
return math.Inf(1)
} else if a.x == other.x {
return 0
}

return math.Abs(a.x-other.x) / math.Abs(a.y-other.y)
}

func (a asteroid) canSee(other asteroid, all []asteroid) bool {
if a.x == other.x && a.y == other.y {
return false
}

for _, blocker := range all {
if (blocker.x == a.x || blocker.x == other.x) && (blocker.y == a.y || blocker.y == other.y) {
continue
} else if blocker.x < math.Min(a.x, other.x) || blocker.x > math.Max(a.x, other.x) {
continue
} else if blocker.y < math.Min(a.y, other.y) || blocker.y > math.Max(a.y, other.y) {
continue
}

if a.slopeTo(blocker) == a.slopeTo(other) {
return a.distanceTo(other) < a.distanceTo(blocker)
}
}

return true
}

func a(challenge *challenge.Input) int {
asteroids := makeMap(challenge)

best := 0
for _, a := range asteroids {
seen := 0
for _, other := range asteroids {
if a.canSee(other, asteroids) {
seen++
}
}

if seen > best {
best = seen
}
}

return best
}

func makeMap(challenge *challenge.Input) []asteroid {
var asteroids []asteroid

y := 0
for line := range challenge.Lines() {
for x, v := range line {
if v == symAsteroid {
asteroids = append(asteroids, asteroid{float64(x), float64(y)})
}
}

y++
}

return asteroids
}
78 changes: 78 additions & 0 deletions challenge/day10/a_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package day10

import (
"strconv"
"testing"

"github.com/nlowe/aoc2019/challenge"
"github.com/stretchr/testify/require"
)

func TestA(t *testing.T) {
tests := []struct {
data string
expected int
}{
{data: `.#..#
.....
#####
....#
...##`, expected: 8},
{data: `......#.#.
#..#.#....
..#######.
.#.#.###..
.#..#.....
..#....#.#
#..#....#.
.##.#..###
##...#..#.
.#....####`, expected: 33},
{data: `#.#...#.#.
.###....#.
.#....#...
##.#.#.#.#
....#.#.#.
.##..###.#
..#...##..
..##....##
......#...
.####.###.`, expected: 35},
{data: `.#..#..###
####.###.#
....###.#.
..###.##.#
##.##.#.#.
....###..#
..#.#..#.#
#..#.#.###
.##...##.#
.....#.#..`, expected: 41},
{data: `.#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##`, expected: 210},
}

for _, tt := range tests {
t.Run(strconv.Itoa(tt.expected), func(t *testing.T) {
require.Equal(t, tt.expected, a(challenge.FromLiteral(tt.data)))
})
}
}
43 changes: 43 additions & 0 deletions challenge/day10/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
....#.....#.#...##..........#.......#......
.....#...####..##...#......#.........#.....
.#.#...#..........#.....#.##.......#...#..#
.#..#...........#..#..#.#.......####.....#.
##..#.................#...#..........##.##.
#..##.#...#.....##.#..#...#..#..#....#....#
##...#.............#.#..........#...#.....#
#.#..##.#.#..#.#...#.....#.#.............#.
...#..##....#........#.....................
##....###..#.#.......#...#..........#..#..#
....#.#....##...###......#......#...#......
.........#.#.....#..#........#..#..##..#...
....##...#..##...#.....##.#..#....#........
............#....######......##......#...#.
#...........##...#.#......#....#....#......
......#.....#.#....#...##.###.....#...#.#..
..#.....##..........#..........#...........
..#.#..#......#......#.....#...##.......##.
.#..#....##......#.............#...........
..##.#.....#.........#....###.........#..#.
...#....#...#.#.......#...#.#.....#........
...####........#...#....#....#........##..#
.#...........#.................#...#...#..#
#................#......#..#...........#..#
..#.#.......#...........#.#......#.........
....#............#.............#.####.#.#..
.....##....#..#...........###........#...#.
.#.....#...#.#...#..#..........#..#.#......
.#.##...#........#..#...##...#...#...#.#.#.
#.......#...#...###..#....#..#...#.........
.....#...##...#.###.#...##..........##.###.
..#.....#.##..#.....#..#.....#....#....#..#
.....#.....#..............####.#.........#.
..#..#.#..#.....#..........#..#....#....#..
#.....#.#......##.....#...#...#.......#.#..
..##.##...........#..........#.............
...#..##....#...##..##......#........#....#
.....#..........##.#.##..#....##..#........
.#...#...#......#..#.##.....#...#.....##...
...##.#....#...........####.#....#.#....#..
...#....#.#..#.........#.......#..#...##...
...##..............#......#................
........................#....##..#........#
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"time"

"github.com/nlowe/aoc2019/challenge/day10"

"github.com/nlowe/aoc2019/challenge/day9"

"github.com/nlowe/aoc2019/challenge/day8"
Expand Down Expand Up @@ -62,6 +64,7 @@ func init() {
day7.A, day7.B,
day8.A, day8.B,
day9.A, day9.B,
day10.A,
)

flags := rootCmd.PersistentFlags()
Expand Down

0 comments on commit a13f9b8

Please sign in to comment.