-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
....#.....#.#...##..........#.......#...... | ||
.....#...####..##...#......#.........#..... | ||
.#.#...#..........#.....#.##.......#...#..# | ||
.#..#...........#..#..#.#.......####.....#. | ||
##..#.................#...#..........##.##. | ||
#..##.#...#.....##.#..#...#..#..#....#....# | ||
##...#.............#.#..........#...#.....# | ||
#.#..##.#.#..#.#...#.....#.#.............#. | ||
...#..##....#........#..................... | ||
##....###..#.#.......#...#..........#..#..# | ||
....#.#....##...###......#......#...#...... | ||
.........#.#.....#..#........#..#..##..#... | ||
....##...#..##...#.....##.#..#....#........ | ||
............#....######......##......#...#. | ||
#...........##...#.#......#....#....#...... | ||
......#.....#.#....#...##.###.....#...#.#.. | ||
..#.....##..........#..........#........... | ||
..#.#..#......#......#.....#...##.......##. | ||
.#..#....##......#.............#........... | ||
..##.#.....#.........#....###.........#..#. | ||
...#....#...#.#.......#...#.#.....#........ | ||
...####........#...#....#....#........##..# | ||
.#...........#.................#...#...#..# | ||
#................#......#..#...........#..# | ||
..#.#.......#...........#.#......#......... | ||
....#............#.............#.####.#.#.. | ||
.....##....#..#...........###........#...#. | ||
.#.....#...#.#...#..#..........#..#.#...... | ||
.#.##...#........#..#...##...#...#...#.#.#. | ||
#.......#...#...###..#....#..#...#......... | ||
.....#...##...#.###.#...##..........##.###. | ||
..#.....#.##..#.....#..#.....#....#....#..# | ||
.....#.....#..............####.#.........#. | ||
..#..#.#..#.....#..........#..#....#....#.. | ||
#.....#.#......##.....#...#...#.......#.#.. | ||
..##.##...........#..........#............. | ||
...#..##....#...##..##......#........#....# | ||
.....#..........##.#.##..#....##..#........ | ||
.#...#...#......#..#.##.....#...#.....##... | ||
...##.#....#...........####.#....#.#....#.. | ||
...#....#.#..#.........#.......#..#...##... | ||
...##..............#......#................ | ||
........................#....##..#........# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters